sfinae

What is “Expression SFINAE”?

百般思念 提交于 2019-11-26 11:14:56
At http://blogs.msdn.com/b/vcblog/archive/2011/09/12/10209291.aspx , the VC++ team officially declare that they have not yet implemented the C++11 core feature "Expression SFINAE". However, The following code examples copied from http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2634.html are accepted by the VC++ compiler. example 1: template <int I> struct A {}; char xxx(int); char xxx(float); template <class T> A<sizeof(xxx((T)0))> f(T){} int main() { f(1); } example 2: struct X {}; struct Y { Y(X){} }; template <class T> auto f(T t1, T t2) -> decltype(t1 + t2); // #1 X f(Y, Y); // #2

SFINAE to check for inherited member functions

主宰稳场 提交于 2019-11-26 11:08:10
Using SFINAE, i can detect wether a given class has a certain member function. But what if i want to test for inherited member functions? The following does not work in VC8 and GCC4 (i.e. detects that A has a member function foo() , but not that B inherits one): #include <iostream> template<typename T, typename Sig> struct has_foo { template <typename U, U> struct type_check; template <typename V> static char (& chk(type_check<Sig, &V::foo>*))[1]; template <typename > static char (& chk(...))[2]; static bool const value = (sizeof(chk<T>(0)) == 1); }; struct A { void foo(); }; struct B : A {};

How to detect existence of a class using SFINAE?

你。 提交于 2019-11-26 11:06:00
问题 Is it possible to detect if a class exists in C++ using SFINAE? If possible then how? Suppose we have a class that is provided only by some versions of a library. I\'d like to know if it is possible to use SFINAE to detect whether the class exists or not. The result of detection is arbitrary, say an enum constant which is 1 if it exists, 0 otherwise. 回答1: If we ask the compiler to tell us anything about a class type T that has not even been declared we are bound to get a compilation error.

check if member exists using enable_if

▼魔方 西西 提交于 2019-11-26 09:49:49
问题 Here\'s what I\'m trying to do: template <typename T> struct Model { vector<T> vertices ; #if T has a .normal member void transform( Matrix m ) { each vertex in vertices { vertex.pos = m * vertex.pos ; vertex.normal = m * vertex.normal ; } } #endif #if T has NO .normal member void transform( Matrix m ) { each vertex in vertices { vertex.pos = m * vertex.pos ; } } #endif } ; I\'ve seen examples of using enable_if , but I cannot understand how to apply enable_if to this problem, or if it even

How to use sfinae for selecting constructors?

蹲街弑〆低调 提交于 2019-11-26 09:42:07
问题 In template meta programming, one can use SFINAE on the return type to choose a certain template member function, i.e. template<int N> struct A { int sum() const noexcept { return _sum<N-1>(); } private: int _data[N]; template<int I> typename std::enable_if< I,int>::type _sum() const noexcept { return _sum<I-1>() + _data[I]; } template<int I> typename std::enable_if<!I,int>::type _sum() const noexcept { return _data[I]; } }; However, this doesn\'t work on constructors. Suppose, I want to

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 =

Selecting a member function using different enable_if conditions

帅比萌擦擦* 提交于 2019-11-26 08:56:24
问题 I am trying to determine which version of a member function gets called based on the class template parameter. I have tried this: #include <iostream> #include <type_traits> template<typename T> struct Point { void MyFunction(typename std::enable_if<std::is_same<T, int>::value, T >::type* = 0) { std::cout << \"T is int.\" << std::endl; } void MyFunction(typename std::enable_if<!std::is_same<T, int>::value, float >::type* = 0) { std::cout << \"T is not int.\" << std::endl; } }; int main() {

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 -

What is decltype with two arguments?

◇◆丶佛笑我妖孽 提交于 2019-11-26 08:11:49
问题 Edit, in order to avoid confusion: decltype does not accept two arguments. See answers. The following two structs can be used to check for the existance of a member function on a type T during compile-time: // Non-templated helper struct: struct _test_has_foo { template<class T> static auto test(T* p) -> decltype(p->foo(), std::true_type()); template<class> static auto test(...) -> std::false_type; }; // Templated actual struct: template<class T> struct has_foo : decltype(_test_has_foo::test

What exactly is the “immediate context” mentioned in the C++11 Standard for which SFINAE applies?

ε祈祈猫儿з 提交于 2019-11-26 05:52:10
问题 Paragraph 14.8.2/8 of the C++11 Standard specifies the conditions under which a substitution failure shall or shall not result in a \"hard\" compilation error (thereby causing compilation to fail) or in a \"soft\" error which would just cause the compiler to discard a template from a set of candidates for overload resolution (without making compilation fail and enabling the well-known SFINAE idiom): If a substitution results in an invalid type or expression, type deduction fails. An invalid