Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing instantiation of unused default function arguments

Lets say I have a function which takes a function pointer as a parameter, and that 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
}

Lets say I want to use register on my class Foo, but Foo doesn't have a default constructor, so my default_construct won't 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 doesn't work. Even though register<Foo> may only be called in one place, and it's passed a function to use, default_construct<Foo> still gets instantiated by the compiler, and I get compiler errors. It seems like since it never gets used, it ought to be skipped over, but I guess that's not the case.

Is there any way to prevent default_construct from being instantiated when it's being used as a default argument? The only solution I can think of is to put it in the template, but it seems like there ought to be a better solution.

like image 933
Alex Avatar asked Aug 10 '11 05:08

Alex


People also ask

What are default arguments give the advantages of using default arguments?

A default argument is a value provided in a function declaration that is automatically assigned by the compiler if the calling function doesn't provide a value for the argument. In case any value is passed, the default value is overridden.

Why must the default argument of function be specified last in the argument list?

Only the argument at the end of the argument list can have default arguments. Because if you insert the default arguments at the middle of the argument list, the compiler would lose track of what arguments went where. void badfunc(int, int=2,int) // Wrong way... void okfunc(int, int=2, int=5); // Correct.

Which operator is used to initialize value for default arguments?

Of the operators, only the function call operator and the operator new can have default arguments when they are overloaded. You can supply any default argument values in the function declaration or in the definition.

Which case default argument passing in function is not allowed?

Default arguments are only allowed in the parameter lists of function declarations and lambda-expressions, (since C++11) and are not allowed in the declarations of pointers to functions, references to functions, or in typedef declarations.


1 Answers

Here's one solution that solves the problem because it doesn't use default arguments:

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>);
}

Note that register is a C++ keyword though :)

like image 181
Seth Carnegie Avatar answered Nov 14 '22 23:11

Seth Carnegie