If I want to write a class that has an optional type parameter I can do the following:
template<typename T> struct X { T t; }; template<> struct X<void> { }; int main() { X<int> a; X<void> b; }; Is there a way to write it so that the void is unnecessary? ie:
int main() { X<int> a; X b; }; I tried this:
template<typename T = void> struct X { T t; }; template<> struct X<void> { }; int main() { X<int> a; X b; }; but I get:
test.cpp: In function ‘int main()’: test.cpp:16:4: error: missing template arguments before ‘b’ test.cpp:16:4: error: expected ‘;’ before ‘b’
X<> b.<>when instantiating your template with default argument, i.e. it has to beX<> b;