Enabling C++ constructor subject to say std::is_floating_point<T>

I’m trying to enable a default constructor only if the class’s template parameter is floating point. Note T is not a parameter type nor return type but the class template type. template <typename T> struct Thing { const T x; Thing( T t) : x(t) {} //only turn on this constructor if T is floating… Read More Enabling C++ constructor subject to say std::is_floating_point<T>

template specialization using enable_if in function definition

if I have the following function struct Struct { template<T> void Foo(); } How can I use enable_if in the function definition without repeating the declaration above? template<T> typename enable_if<is_class<T>,void>::type Struct::Foo() { … } // error: Struct has no member `Foo<T>` template<T> typename enable_if<!is_class<T>,void>::type Struct::Foo() { … } // error: Struct has no member `Foo<T>`… Read More template specialization using enable_if in function definition

Why when use enable_if in class tempalte have to set the second parameter's default type as void?

I’m currently studying enable_if. But I have this code: //template<typename T, typename = int/double/float/…> //not working properly template<typename T, typename = void> //works fine struct test{ void func(){ cout << "default" << endl; } }; template<typename T> struct test<T, typename std::enable_if<(sizeof(T) <= 1)>::type>{ void func(){ cout << "called" << endl; } }; int main() {… Read More Why when use enable_if in class tempalte have to set the second parameter's default type as void?

How does typename assignment work in C++ (typename =)?

I came across this example when looking at std::enable_if: template<class T, typename = std::enable_if_t<std::is_array<T>::value> > void destroy(T* t) { for(std::size_t i = 0; i < std::extent<T>::value; ++i) { destroy((*t)[i]); } } In template argument lists, you can put untemplated classes/structs. So the above code is still possible when we remove the typename =. What does… Read More How does typename assignment work in C++ (typename =)?