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;
All data types, both primitive and compound types, must be defined by using a template.
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.
There is no difference. typename and class are interchangeable in the declaration of a type template parameter.
" 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.
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.
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