Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a nonstatic member of a template specialize to data or function?

GCC, Clang, ICC, and MSVC all reject this code, but I don't find any violated rule in the latest working draft of the C++ standard.

Is the rule already in the standard, or is it in a defect report?

#include <type_traits>

template< typename t >
struct s {
    std::conditional_t< std::is_integral< t >::value, t, void() > mem;
};

s< int > a;
s< void * > b;
like image 602
Potatoswatter Avatar asked Mar 18 '15 01:03

Potatoswatter


People also ask

Which of the data types are supported by template?

All data types, both primitive and compound types, must be defined by using a template.

How will you restrict the template for a specific datatype?

There are ways to restrict the types you can use inside a template you write by using specific typedefs inside your template. This will ensure that the compilation of the template specialisation for a type that does not include that particular typedef will fail, so you can selectively support/not support certain types.

What is the difference between template Typename T and template T?

There is no difference. typename and class are interchangeable in the declaration of a type template parameter.

What does template Typename do?

" typename " is a keyword in the C++ programming language used when writing templates. It is used for specifying that a dependent name in a template definition or declaration is a type.


1 Answers

The code is invalid due to 14.3.1/3:

If a declaration acquires a function type through a type dependent on a template-parameter and this causes a declaration that does not use the syntactic form of a function declarator to have function type, the program is ill-formed.

The type of the declaration here is dependent on the template parameter t, and therefore cannot be a function type.

like image 162
interjay Avatar answered Oct 19 '22 20:10

interjay