Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++1y/C++14: Variable Template Specialization?

According to C++1y/C++14 N3690, does the type of a variable template specialization have to be the same as the type of the primary template?

template<int x> char y = f(x);  template<> double y<42> = g(); 

And if so, is it possible to leave the primary undefined somehow?

template<int x> ???? y = ???; // undefined  template<> double y<42> = g(); 

Where is this covered in the draft?

The equivalent functionality for a class template would be:

template<int x> struct S {     static char y; };  template<> struct S<42> {     static double y; }; 

and

template<int x> struct S; // undefined  template<> struct S<42> {     static double y; }; 
like image 672
Andrew Tomazos Avatar asked Oct 01 '13 05:10

Andrew Tomazos


People also ask

Is specialization of template C++?

The act of creating a new definition of a function, class, or member of a class from a template declaration and one or more template arguments is called template instantiation. The definition created from a template instantiation is called a specialization.

How do you declare a template variable in C++?

A variable template may be introduced by a template declaration at namespace scope, where declaration declares a variable. When used at class scope, variable template declares a static data member template.

What is typename C++?

Use the keyword typename if you have a qualified name that refers to a type and depends on a template parameter. Only use the keyword typename in template declarations and definitions.

How do you call a function template in C++?

Defining a Function TemplateA function template starts with the keyword template followed by template parameter(s) inside <> which is followed by the function definition. In the above code, T is a template argument that accepts different data types ( int , float , etc.), and typename is a keyword.


1 Answers

Does the type of a variable template specialization have to be the same as the type of the primary template?

No, an explicit (or partial) specialization of a variable template can specify a type different from the type that would be implied by an implicit instantiation. When implementing the feature for Clang, we discovered that the specification had no rule requiring the type to match in this case, and we brought the issue to the C++ core working group, where it was confirmed that this omission was intentional.

Is it possible to leave the primary undefined somehow?

It is not possible to declare the primary variable template without specifying a type -- there is no syntax that would allow such a thing.

Where is this covered in the draft?

Both of these are covered by omission -- there is no rule requiring the types to match, and there is no syntax for declaring a variable template without a type. So I can't point you at any particular part of the standard and say "here's where the rule isn't".

If you have access to the C++ standard committee's reflectors, see the thread starting with core-23901 for the discussion of this.

like image 89
Richard Smith Avatar answered Sep 24 '22 15:09

Richard Smith