Consider a template class like this:
template <int opt1 = 1,
bool opt2 = false,
bool opt3 = true,
int opt4 = 50>
class X { };
I try to change just one parameter, but it seems that C++ can’t do that. Or am I wrong? How to achieve something like this:
X<opt2 = true> x;
The Boost version mentioned by R. Martinho Fernandes works something like this:
define a number of option types (they can be empty tag structures, or templated on
boolto enable/disable, or whatever)define some kind of typelist of your default values (I’ll hide it in a namespace along with the existing template class)
write a
getmechanism to extract an option from a suitable typelist (including a version which falls back on the provided default)Once you have all that, you can write a wrapper class that handles all the options and delegates to your existing class, eg.
Your final invocation now looks like:
The
typelistandgetparts are left as an exercise (along with lots of errors, most likely) for the OP, a kindly passing editor, or me when I have more time.