Info
I am trying to use a template alias to improve the readabilty of my code. Ideally I would like to the alias to have a default argument such that if I leave out the template it uses the default (exactly with template functions and template classes).
The code would look like
template<typename T = double>
struct mystruct {};
template<typename T = double> using myalias = mystruct<T>;
int main(void) {
myalias MyStructWithDouble; // causes compilation error
myalias<int> MyStructWithInt;
return 0;
}
The compiler (g++ 4.7 in this case) is quite happy with the inclusion of the = double
in the alias definition but it seems to then ignore this.
I tried something like "specializing" the alias as well but there the compiler baulked.
Question
Why does the compiler accept the default in the defintion if we are not allowed to use it? Secondly, is there a way of acheiveing this?
Motivation
This example is very simple but in my real code the alias would save a lot of typing (there are more than one template parameter)
Just like with class templates, you still need to supply an empty template argument list:
myalias<> MyStructWithDouble; // compiles
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