sfinae

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

C++ SFINAE examples?

狂风中的少年 提交于 2019-11-26 04:29:47
问题 I want to get into more template meta-programming. I know that SFINAE stands for \"substitution failure is not an error.\" But can someone show me a good use for SFINAE? 回答1: Heres one example (from here): template<typename T> class IsClassT { private: typedef char One; typedef struct { char a[2]; } Two; template<typename C> static One test(int C::*); // Will be chosen if T is anything except a class. template<typename C> static Two test(...); public: enum { Yes = sizeof(IsClassT<T>::test<T>

How to check whether operator== exists?

不打扰是莪最后的温柔 提交于 2019-11-26 03:39:10
问题 I am trying to create an example, which would check the existence of the operator== (member or, non-member function). To check whether a class has a member operator== is easy, but how to check whether it has a non-member operator== ? This is what I have to far : #include <iostream> struct A { int a; #if 0 bool operator==( const A& rhs ) const { return ( a==rhs.a); } #endif }; #if 1 bool operator==( const A &l,const A &r ) { return ( l.a==r.a); } #endif template < typename T > struct

Metaprograming: Failure of Function Definition Defines a Separate Function

混江龙づ霸主 提交于 2019-11-26 02:24:53
问题 In this answer I define a template based on the type\'s is_arithmetic property: template<typename T> enable_if_t<is_arithmetic<T>::value, string> stringify(T t){ return to_string(t); } template<typename T> enable_if_t<!is_arithmetic<T>::value, string> stringify(T t){ return static_cast<ostringstream&>(ostringstream() << t).str(); } dyp suggests that rather than the is_arithmetic property of the type, that whether to_string is defined for the type be the template selection criteria. This is

SFINAE to check for inherited member functions

那年仲夏 提交于 2019-11-26 02:17:37
问题 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(...))

How to detect whether there is a specific member variable in class?

心不动则不痛 提交于 2019-11-26 01:38:48
问题 For creating algorithm template function I need to know whether x or X (and y or Y) in class that is template argument. It may by useful when using my function for MFC CPoint class or GDI+ PointF class or some others. All of them use different x in them. My solution could be reduces to the following code: template<int> struct TT {typedef int type;}; template<class P> bool Check_x(P p, typename TT<sizeof(&P::x)>::type b = 0) { return true; } template<class P> bool Check_x(P p, typename TT

Why do constant expressions have an exclusion for undefined behavior?

隐身守侯 提交于 2019-11-26 00:48:27
问题 I was researching what is allowed in a core constant expression*, which is covered in section 5.19 Constant expressions paragraph 2 of the draft C++ standard which says: A conditional-expression is a core constant expression unless it involves one of the following as a potentially evaluated subexpression (3.2), but subexpressions of logical AND (5.14), logical OR (5.15), and conditional (5.16) operations that are not evaluated are not considered [ Note: An overloaded operator invokes a

Is it possible to write a template to check for a function&#39;s existence?

僤鯓⒐⒋嵵緔 提交于 2019-11-25 23:55:46
问题 Is it possible to write a template that changes behavior depending on if a certain member function is defined on a class? Here\'s a simple example of what I would want to write: template<class T> std::string optionalToString(T* obj) { if (FUNCTION_EXISTS(T->toString)) return obj->toString(); else return \"toString not defined\"; } So, if class T has toString() defined, then it uses it; otherwise, it doesn\'t. The magical part that I don\'t know how to do is the \"FUNCTION_EXISTS\" part. 回答1:

SFINAE working in return type but not as template parameter

半世苍凉 提交于 2019-11-25 23:47:48
问题 I already used the SFINAE idiom quite a few times and I got used to put my std::enable_if<> in template parameters rather than in return types. However, I came across some trivial case where it didn\'t work, and I\'m not sure why. First of all, here is my main: int main() { foo(5); foo(3.4); } Here is an implementation of foo that triggers the error: template<typename T, typename = typename std::enable_if<std::is_integral<T>::value>::type> auto foo(T) -> void { std::cout << \"I\'m an integer!

How does `void_t` work

微笑、不失礼 提交于 2019-11-25 23:39:12
问题 I watched Walter Brown\'s talk at Cppcon14 about modern template programming (Part I, Part II) where he presented his void_t SFINAE technique. Example: Given a simple variable template that evaluates to void if all template arguments are well formed: template< class ... > using void_t = void; and the following trait that checks for the existence of a member variable called member : template< class , class = void > struct has_member : std::false_type { }; // specialized as has_member< T , void