enable-if

Why doesn't SFINAE (enable_if) work for member functions of a class template?

南楼画角 提交于 2019-11-27 08:41:12
#include <type_traits> struct A{}; struct B{}; template <typename T> struct Foo { typename std::enable_if<std::is_same<T, A>::value>::type bar() {} typename std::enable_if<std::is_same<T, B>::value>::type bar() {} }; Error message: 14:5: error: 'typename std::enable_if<std::is_same<T, B>::value>::type Foo<T>::bar()' cannot be overloaded 10:5: error: with 'typename std::enable_if<std::is_same<T, A>::value>::type Foo<T>::bar()' Source on cpp.sh . I thought both typename std::enable_if<std::is_same<T,?>::value>::type could not be valid at the same time. Edit For posterity here is my edit based on

Why compile error with enable_if

一世执手 提交于 2019-11-27 02:04:29
问题 Why this does not compile with gcc48 and clang32? #include <type_traits> template <int N> struct S { template<class T> typename std::enable_if<N==1, int>::type f(T t) {return 1;}; template<class T> typename std::enable_if<N!=1, int>::type f(T t) {return 2;}; }; int main() { S<1> s1; return s1.f(99); } GCC error: /home/lvv/p/sto/test/t.cc:12:2: error: no type named ‘type’ in ‘struct enable_if<false, int>’ f(T t) {return 2;}; ^ CLANG error: /home/lvv/p/sto/test/t.cc:11:26: error: no type named

C++11: Disambiguate class-member in multiple inheritance

夙愿已清 提交于 2019-11-26 22:59:18
Suppose I have this variadic base class-template: template <typename ... Types> class Base { public: // The member foo() can only be called when its template // parameter is contained within the Types ... pack. template <typename T> typename std::enable_if<Contains<T, Types ...>::value>::type foo() { std::cout << "Base::foo()\n"; } }; The foo() member can only be called when its template-parameter matches at least one of the parameters of Base (the implementation of Contains is listed at the bottom at this post): Base<int, char>().foo<int>(); // fine Base<int, char>().foo<void>(); // error Now

Why does enable_if_t in template arguments complains about redefinitions?

試著忘記壹切 提交于 2019-11-26 20:33:44
问题 I have the following case that works using std::enable_if : template<typename T, typename std::enable_if<std::is_same<int, T>::value>::type* = nullptr> void f() { } template<typename T, typename std::enable_if<std::is_same<double, T>::value>::type* = nullptr> void f() { } Now, I saw in cppreference the new syntax, much cleaner in my opinion : typename = std::enable_if_t<std::is_same<int, T>::value>> I wanted to port my code : template<typename T, typename = std::enable_if_t<std::is_same<int,

Default template argument when using std::enable_if as templ. param.: why OK with two template functions that differ only in the enable_if parameter?

我们两清 提交于 2019-11-26 16:54:01
问题 In the language reference of std::enable_if at cppreference the following note is included Notes A common mistake is to declare two function templates that differ only in their default template arguments. This is illegal because default template arguments are not part of function template's signature, and declaring two different function templates with the same signature is illegal. In the template functions in example below, it seems to me that this situation occurs. I.e., the two template

How Does std::enable_if work?

泄露秘密 提交于 2019-11-26 14:16:04
问题 I just asked this question: std::numeric_limits as a Condition I understand the usage where std::enable_if will define the return type of a method conditionally causing the method to fail to compile. template<typename T> typename std::enable_if<std::numeric_limits<T>::is_integer, void>::type foo(const T &bar) { isInt(bar); } What I don't understand is the second argument and the seemingly meaningless assignment to std::enable_if when it's declared as part of the template statement, as in

How to write a type trait `is_container` or `is_vector`?

元气小坏坏 提交于 2019-11-26 09:26:13
问题 Is it possible to write a type trait whose value is true for all common STL structures (e.g., vector , set , map , ...)? To get started, I\'d like to write a type trait that is true for a vector and false otherwise. I tried this, but it doesn\'t compile: template<class T, typename Enable = void> struct is_vector { static bool const value = false; }; template<class T, class U> struct is_vector<T, typename boost::enable_if<boost::is_same<T, std::vector<U> > >::type> { static bool const value =

Template specialization and enable_if problems [duplicate]

别等时光非礼了梦想. 提交于 2019-11-26 08:19:42
问题 This question already has answers here : SFINAE working in return type but not as template parameter (3 answers) Closed 4 years ago . I am running into a problem regarding the appropriate usage of enable_if and template specialization. After modifying the example (for confidentiality reasons), here\'s a comparable example: I have function called \"less\" that checks if 1st arg is less than 2nd arg. Let\'s say I want to have 2 different kinds of implementations depending on the type of input -

Why should I avoid std::enable_if in function signatures

三世轮回 提交于 2019-11-26 04:57:37
问题 Scott Meyers posted content and status of his next book EC++11. He wrote that one item in the book could be \"Avoid std::enable_if in function signatures\" . std::enable_if can be used as a function argument, as a return type or as a class template or function template parameter to conditionally remove functions or classes from overload resolution. In this question all three solution are shown. As function parameter: template<typename T> struct Check1 { template<typename U = T> U read