Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will it be possible to omit the type name when initializing C++2a class non-type template arguments?

Tags:

c++

c++20

The following does not compile on GCC 9.1 (which supports class non-type template parameters)

struct S { int i; };

template<S s>
struct T {};

int main()
{
    T<{0}> x{};
}

The compiler reports error: could not convert '{0}' from '<brace-enclosed initializer list>' to 'S' despite the template argument s being of concrete type S.

T<S{0}> x{};

works as expected, but will C++2a allow the concrete type name S to be omitted, as is the case in other parts of the language?

like image 840
invexed Avatar asked Jul 22 '19 08:07

invexed


1 Answers

will C++2a allow the concrete type name S to be omitted?

No.

[temp.arg.nontype]/2

A template-argument for a non-type template-parameter shall be a converted constant expression ([expr.const]) of the type of the template-parameter.

In T<{0}>, {0} is not an S: it is not an expression of the type of the template-parameter (S). {0} would be an initializer list (in a context where it would be allowed).


Bonus:

[dcl.init.list]/4

List-initialization can occur in direct-initialization or copy-initialization contexts; list-initialization in a direct-initialization context is called direct-list-initialization and list-initialization in a copy-initialization context is called copy-list-initialization.

No initialization occur for template-arguments (unless when it does, see [temp.arg.nontype]/1).

like image 113
YSC Avatar answered Nov 11 '22 05:11

YSC