enable-if

Using enable_if with struct specialization

…衆ロ難τιáo~ 提交于 2021-02-19 01:16:36
问题 I am attempting to define a template that will will specify a storage type given another type T. I'd like to use enable_if to catch all the arithmetic types. Below is my attempt at this which complains the template is redeclared with 2 parameters. I tried adding a 2nd dummy parm to the primary template but get a different error. How can this be done? #include <string> #include <type_traits> template <typename T> struct storage_type; // want compile error if no match // template <typename T,

Using enable_if with struct specialization

爷,独闯天下 提交于 2021-02-19 01:15:48
问题 I am attempting to define a template that will will specify a storage type given another type T. I'd like to use enable_if to catch all the arithmetic types. Below is my attempt at this which complains the template is redeclared with 2 parameters. I tried adding a 2nd dummy parm to the primary template but get a different error. How can this be done? #include <string> #include <type_traits> template <typename T> struct storage_type; // want compile error if no match // template <typename T,

Using enable_if with struct specialization

拟墨画扇 提交于 2021-02-19 01:15:34
问题 I am attempting to define a template that will will specify a storage type given another type T. I'd like to use enable_if to catch all the arithmetic types. Below is my attempt at this which complains the template is redeclared with 2 parameters. I tried adding a 2nd dummy parm to the primary template but get a different error. How can this be done? #include <string> #include <type_traits> template <typename T> struct storage_type; // want compile error if no match // template <typename T,

Using enable_if with struct specialization

跟風遠走 提交于 2021-02-19 01:15:24
问题 I am attempting to define a template that will will specify a storage type given another type T. I'd like to use enable_if to catch all the arithmetic types. Below is my attempt at this which complains the template is redeclared with 2 parameters. I tried adding a 2nd dummy parm to the primary template but get a different error. How can this be done? #include <string> #include <type_traits> template <typename T> struct storage_type; // want compile error if no match // template <typename T,

Partial template function specialization with enable_if: make default implementation

こ雲淡風輕ζ 提交于 2021-02-17 18:54:10
问题 Using C++11's enable_if I want to define several specialized implementations for a function (based on the type of the parameter, say) as well as a default implementation. What is the correct way to define it? The following example does not work as intended since the "generic" implementation is called, whatever the type T . #include <iostream> template<typename T, typename Enable = void> void dummy(T t) { std::cout << "Generic: " << t << std::endl; } template<typename T, typename std::enable

Add method to class by template parameter

女生的网名这么多〃 提交于 2021-02-07 22:51:12
问题 I would like to have a template parameter specific function inside a class unsing enable_if. Its name stays the same, the parameter type varies (although this should not be relevant since only one is initialized). enum class MyCases { CASE1, CASE2 }; template<enum MyCases case> class MyClass { template<typename = typename std::enable_if<case == MyCases::CASE1>::type> void myFunction(ParameterTypeA a) { ... } template<typename = typename std::enable_if<case == MyCases::CASE2>::type> void

Add method to class by template parameter

戏子无情 提交于 2021-02-07 22:51:08
问题 I would like to have a template parameter specific function inside a class unsing enable_if. Its name stays the same, the parameter type varies (although this should not be relevant since only one is initialized). enum class MyCases { CASE1, CASE2 }; template<enum MyCases case> class MyClass { template<typename = typename std::enable_if<case == MyCases::CASE1>::type> void myFunction(ParameterTypeA a) { ... } template<typename = typename std::enable_if<case == MyCases::CASE2>::type> void

Add method to class by template parameter

帅比萌擦擦* 提交于 2021-02-07 22:50:34
问题 I would like to have a template parameter specific function inside a class unsing enable_if. Its name stays the same, the parameter type varies (although this should not be relevant since only one is initialized). enum class MyCases { CASE1, CASE2 }; template<enum MyCases case> class MyClass { template<typename = typename std::enable_if<case == MyCases::CASE1>::type> void myFunction(ParameterTypeA a) { ... } template<typename = typename std::enable_if<case == MyCases::CASE2>::type> void

enable_if function defined when it shouldn't be

风流意气都作罢 提交于 2021-02-04 16:41:09
问题 As an experiment, I am trying to make a void member function with no parameters change behavior based on the class template parameter: #include <iostream> #include <limits> template<typename T> class MyClass { public: void MyFunc(const typename std::enable_if<std::is_fundamental<T>::value, T> dummy = T()); void MyFunc(const typename std::enable_if<!std::is_fundamental<T>::value, T> dummy = T()); }; template<typename T> void MyClass<T>::MyFunc(const typename std::enable_if<std::is_fundamental

Why does this SFINAE not work with enable_if when one conditional branch is inherited from the base class?

江枫思渺然 提交于 2021-01-28 06:25:30
问题 #include <bits/stdc++.h> #include <type_traits> // Type your code here, or load an example. template <typename Types> class C1 { public: using A=typename Types::A; using B=typename Types::B; template <typename Dummy = void> inline typename std::enable_if<std::is_same<A, B>::value, Dummy>::type f() { } }; template <typename Types> class C2 : public C1<Types> { public: using A=typename Types::A; using B=typename Types::B; template <typename Dummy = void> inline typename std::enable_if<!std::is