Preventing the Creation of Unused Function Arguments by Default

Lets say that I have a function that takes a pointer to a function as a parameter, and this parameter has a default argument.

template <typename T>
T* default_construct()
{
    return new T();
}

template <typename T>
void register(T* (*construct)() = default_construct<T>)
{
    // Save that function pointer for later
}

Suppose I want to use case in my class Foo, but Foodoes not have a default constructor, so mine default_constructwill not work on it. The obvious solution is to do something like this:

Foo* construct_Foo()
{
    return new Foo("String argument", 123);
}

SomeFunc()
{
    // ...
    register<Foo>(construct_Foo);
    // ...
}

But that does not work. Although it register<Foo>can only be called in one place, and it passed the function to use, default_construct<Foo>it still gets the compiler instance, and I get compiler errors. It seems that since it is never used, it should be skipped, but I think it is not.

default_construct ? , , , , .

+5
1

, , :

template <typename T>
T* default_construct()
{
    return new T();
}

template <typename T>
void register(T* (*construct)())
{
    // Save that function pointer for later
}

template<typename T>
void register()
{
    register<T>(default_construct<T>);
}

, register ++:)

+6

All Articles