Skip to main content
added 228 characters in body
Source Link
max66
  • 67k
  • 11
  • 76
  • 121

If you want enable/disable the full class/struct, you can use SFINAE and partial specialization.

The following is a C++17 example

template <typename T, typename = void> struct testClass; template <typename T> struct testClass<T, std::void_t<decltype(T::value)>> { testClass() { }; T parameter; void print() { std::cout << parameter.value << std::endl; } }; 

If you only want enable/disable the print() function, you have to templatize it; by example

template <typename U = T> std::void_t<decltype(U::value)> print() { std::cout << parameter.value << std::endl; } 

or also

template <typename U = T> std::void_t<decltype(U::value), std::enable_if_t<std::is_same_v<U, T>>> print() { std::cout << parameter.value << std::endl; } 

if you want to be sure that nobody can "hijack" the method explicating the template type calling

testClass<paramtype3>{}.print<paramtype1>(): 

If you want enable/disable the full class/struct, you can use SFINAE and partial specialization.

The following is a C++17 example

template <typename T, typename = void> struct testClass; template <typename T> struct testClass<T, std::void_t<decltype(T::value)>> { testClass() { }; T parameter; void print() { std::cout << parameter.value << std::endl; } }; 

If you want enable/disable the full class/struct, you can use SFINAE and partial specialization.

The following is a C++17 example

template <typename T, typename = void> struct testClass; template <typename T> struct testClass<T, std::void_t<decltype(T::value)>> { testClass() { }; T parameter; void print() { std::cout << parameter.value << std::endl; } }; 

If you only want enable/disable the print() function, you have to templatize it; by example

template <typename U = T> std::void_t<decltype(U::value)> print() { std::cout << parameter.value << std::endl; } 

or also

template <typename U = T> std::void_t<decltype(U::value), std::enable_if_t<std::is_same_v<U, T>>> print() { std::cout << parameter.value << std::endl; } 

if you want to be sure that nobody can "hijack" the method explicating the template type calling

testClass<paramtype3>{}.print<paramtype1>(): 
Source Link
max66
  • 67k
  • 11
  • 76
  • 121

If you want enable/disable the full class/struct, you can use SFINAE and partial specialization.

The following is a C++17 example

template <typename T, typename = void> struct testClass; template <typename T> struct testClass<T, std::void_t<decltype(T::value)>> { testClass() { }; T parameter; void print() { std::cout << parameter.value << std::endl; } };