I can’t figure out why
std::enable_if<is_allowed_type<T>::value>::type*=nullptr
does not work, while
std::enable_if<is_allowed_type<T>::value>::type* =nullptr
works well.
The difference is just a blank space. Does the *=
is treated as an operator?
Here is the code snippet:
#include <stdint.h>
#include <type_traits>
template<class T>
class is_allowed_type:public std::false_type{};
template<>
class is_allowed_type<uint8_t>:public std::true_type{};
template<>
class is_allowed_type<uint16_t>:public std::true_type{};
template<class T>
typename std::enable_if<is_allowed_type<T>::value>::type foo(T val){}
template<class T, typename = typename std::enable_if<is_allowed_type<T>::value>::type>
void goo(T val){}
template<class T>
void koo(T val, typename std::enable_if<is_allowed_type<T>::value>::type*=nullptr){}
template<class T>
void hoo(T val, typename std::enable_if<is_allowed_type<T>::value>::type* = nullptr){}
int main() {
unsigned char val{'0'};
uint16_t num{0};
foo(val); //works;
foo(num); //works;
//foo(6); //not compile as expected
//foo(3.14); //not compile as expected
return 0;
}
Here is what the compiler complains:
<source>:20:73: error: expected ')'
20 | void koo(T val, typename std::enable_if<is_allowed_type<T>::value>::type*=nullptr){}
| ^
<source>:20:9: note: to match this '('
20 | void koo(T val, typename std::enable_if<is_allowed_type<T>::value>::type*=nullptr){}
| ^
1 error generated.
1