Default template arguments for C ++ 11 'using' type alias

I want to use a type alias so that I 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 ++ for some reason does not approve:

test.cpp: In functionint main()’:
test.cpp:12:13: error: missing template arguments beforevval v;      //should now be equal to val<float> v;
             ^
test.cpp:12:13: error: expected ‘;’ before ‘v’

Do not the default template arguments work with 'using'? If so, why doesn't he talk about it on the line, is the default argument specified?

+4
source share
2 answers

Introduction

Presence of default values ​​for template parameters in alias template legal, but you cannot leave <and >when later use the specified 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>

What does the standard say? ( n3337 )

14.5.7p1 Nickname Templates [temp.alias]

, alias ( 7), . - . - -.

, , -, , -.

14.2p1 [temp.names]

simple-template-id:
  template-name < template-argument-list_opt >

template-name:
  identifier

. , <> simple-template-id , .

+10

<>. :

    val<> v;    //should now be equal to val<float> v;
+3

Source: https://habr.com/ru/post/1544571/


All Articles