I want to alias a type so that it can be given a template argument if necessary.
template<typename T, unsigned d>
struct value
{
T a[d];
};
template<typename T=float>
using val=value<T, 2>;
int main()
{
val v; //should now be equal to val<float> v;
val<int> w; //should also be valid.
return 0;
}
G++ does not approve for some reason:
test.cpp: In function ‘int main()’:
test.cpp:12:13: error: missing template arguments before ‘v’
val v; //should now be equal to val<float> v;
^
test.cpp:12:13: error: expected ‘;’ before ‘v’
Do default template arguments not work with 'using'? If so, why does it not say so on the line the default argument is specified on?
Can default arguments be used with the template class? Explanation: The template class can use default arguments.
Type alias is a name that refers to a previously defined type (similar to typedef). Alias template is a name that refers to a family of types.
Like function default arguments, templates can also have default arguments. For example, in the following program, the second parameter U has the default value as char.
A template argument for a template template parameter is the name of a class template. When the compiler tries to find a template to match the template template argument, it only considers primary class templates. (A primary template is the template that is being specialized.)
Having default values for template-parameters in a alias-template is legal, but you cannot leave out <
, and >
, when you are later using said alias.
template<class T = float>
using val = value<T, 2>;
val<> v; // legal, decltype(v) => value<float, 2>
val<int> w; // legal, decltype(w) => value<int, 2>
14.5.7p1
Alias templates[temp.alias]
A template-declaration in which the declaration is an alias-declaration (Clause 7) declares the identifier to be a alias-template. An alias template is a name for a family of types. The name of the alias template is a template-name.
The above states that the name introduced by a template-alias is a template-name, and a template-name must be followed by a template-argument-list.
14.2p1
Names of template specialization[temp.names]
simple-template-id: template-name < template-argument-list_opt > template-name: identifier
Note: Notice how the two <>
are not optional when referring to a simple-template-id, and that a template-name by itself is merely an identifier, not a type.
You can use only function templates without <>
. For classes empty list of template parameters is necessary:
val<> v; //should now be equal to val<float> v;
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