Вопрос

This seems so simple that it shouldn't be a problem, but I'm not seeing it, so any help would be appreciated.

I have a function that takes a parameter, then uses that parameter in a template. The compiler (Visual C++ 2012) is generating a C4100 "Unreferenced Formal Parameter" warning...and this is annoying. I cannot see why it thinks the parameter is unreferenced.

#define FactoryRegister(f, T) \
{ \
    shared_ptr<FactoryCreator<T>> creator(new FactoryCreator<T>()); \
    f.instance().Register(#T, creator); \
    f.instance().Register(typeid(T).name(), creator); \
}

void FactoryRegister(hive::Factory& factory)
{
    FactoryRegister(factory, MyClass);
}

Compiling this generates: "warning C4100: 'factory' : unreferenced formal parameter"

I would have thought that "factory" is quite obviously referenced as its "instance()" function is called twice after macro expansion. I assume here that the code expands to:

void FactoryRegister(Factory& factory)
{
    shared_ptr<FactoryCreator<MyClass>> creator(new FactoryCreator<MyClass>());
    factory.instance().Register("MyClass", creator);
    factory.instance().Register(typeid(MyClass).name(), creator);
}

The code functions fine, its just throwing a warning that I don't understand. Note that, in my actual code, the macro and the function are in different projects and different files, but they were included here together for simplicity.

Это было полезно?

Решение

Based on the instance name, it looks like factory might be a singleton, and instance is a static function. In that case, the value of factory is indeed never used, and you can instead call the function directly on the class, which is essentially what the compiler does anyway:

Factory::instance().Register(...);

Then you can remove the parameter entirely, and you'll avoid the apparent compiler bug that conflates usage of the parameter with usage of the parameter's value.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top