You could specialize your type so that optional_args
doesn't exist when the number is zero. If you need the object to exist then the only way in which an object can exist and can be referred to while in fact taking up no space is through the empty base class optimization.
You might use that in the following way:
template<int num_optional_args>
class optional_args {
std::array<int,num_optional_args> args
public:
// whatever interface you want for the optional args.
void foo(int n) {
if (n < num_optional_args)
args[n];
throw std::runtime_error("out of range");
}
};
template<>
class optional_args<0> {
public:
// whatever interface you want for the optional args, specialized for 0 args.
void foo(int n) {
throw std::runtime_error("out of range");
}
};
template<int num_optional_args>
class C : optional_args<num_optional_args> {
int some_variable;
void bar() {
for (int i=0; i<num_optional_args; ++i) {
optional_args::foo(i);
}
}
};