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.
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.
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.
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.
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.
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With