sfinae

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

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-13 04:09:24
问题 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

Using enable_if and underlying_type in function signature in VS2012

邮差的信 提交于 2019-12-13 03:55:34
问题 This code works in VS2013 and other compilers (tested clang 3.4 and gcc 4.8) but fails to compile in VS2012: #include <type_traits> #include <cstdio> // error C4519: default template arguments are only allowed on a class template template<typename E, typename std::enable_if<std::is_enum<E>::value>::type* = nullptr> typename std::underlying_type<E>::type to_integral(E e) { return static_cast<typename std::underlying_type<E>::type>(e); } template<typename E, typename std::enable_if<!std::is

Why I can't use inheritance's error as SFINAE?

六眼飞鱼酱① 提交于 2019-12-13 02:43:32
问题 I have this code, but it does not compile: #include <iostream> #include <stdexcept> #include <cassert> template< class > struct id{}; template< class U, class V> struct base: public id<U>, public id<V> { static const bool value = true; }; template< class U, class V> struct is_different { typedef char (&true_)[1]; typedef char (&false_)[2]; template< class T, class K, bool = base<T,K>::value > struct checker; template< class T, class K> static true_ test( checker<T,K>* ); template< class ,

Workaround for lack of expression SFINAE

好久不见. 提交于 2019-12-13 00:43:02
问题 I'm trying to call a function for each value in a std::tuple , of course there is no way to iterate a tuple and so I've resorted to using the template techniques discussed in iterate over tuple However, I'm using Visual Studio 2013 and it does not support expression SFINAE, so that code won't work. I've tried to partially specialize the templates based on a constant numbers (e.g 5, 4, 3, 2, 1, 0), but haven't had any success. I'm certainly no template expert, and so I was hoping somebody

How to write template overload functions with fallback triggered if template arguments do not allow instantiation of a certain class

柔情痞子 提交于 2019-12-12 17:09:35
问题 The program below does not compile if I uncomment the line containing foo<double>() , because B<double> depends on A<double> , which is an incomplete type. #include <iostream> using namespace std; template <class T> struct A; // forward declaration (incomplete) template <> struct A<int> {}; // specialized for int template <class T> struct B : A<T> { int foo() {return 0;} }; // derived class, general definition inherits from A template <> struct B<bool> { int foo() {return 1;} }; // derived

function resolution failed when return type is deduced from enclosed template class

家住魔仙堡 提交于 2019-12-12 15:11:19
问题 I have been trying to implement a complex number class for fixed point types where the result type of the multiply operation will be a function of the input types. I need to have functions where I can do multiply complex by complex and also complex by real number. This essentially is a simplified version of the code. Where A is my complex type. template<typename T1, typename T2> struct rt {}; template<> struct rt<double, double> { typedef double type; }; //forward declaration template

Specializing class with SFINAE

社会主义新天地 提交于 2019-12-12 12:28:26
问题 I want to write a template class which checks a trait with SFINAE. As classes can not be "overloaded" as I read in that post: template overloading and SFINAE working only with functions but not classes I wrote the following code: class AA { public: using TRAIT = int; }; class BB { public: using TRAIT = float; }; template < typename T, typename UNUSED = void> class X; template < typename T> class X<T, typename std::enable_if< std::is_same< int, typename T::TRAIT>::value, int >::type> { public:

sfinae on member function defined outside of class body

被刻印的时光 ゝ 提交于 2019-12-12 12:24:11
问题 Kind of a continuation from my previous question. What I've got is a bunch of functions that form a sfinae depencency chain like so (let "A -> B" notation mean that existence of A depends on existence of B): S::f_base -> S::f -> ns::f_ -> f -> T::f where T is the template argument. It's implemented like this: #include <utility> struct S; template <typename T> auto f(S& s, T const& t) -> decltype(t.f(s), void()) { t.f(s); } namespace ns { template <typename T> auto f_(S& s, T const& t) ->

Why does the following program not select the argument of the same type as the first template parameter?

依然范特西╮ 提交于 2019-12-12 10:45:41
问题 I am trying to write a function such that f<T>(args..) returns the first parameter of type T . The following program seems to always select the first specialization thus printing 97 (ASCII code of 'a' ). Though the second one wouldn't require converting char to int . Could someone please explain the behavior? I am new to SFINAE and meta-programming. #include <iostream> using namespace std; template <typename T, typename ...Ts> T f(T a, Ts... args) { return a; } template <typename R, typename

Template method to select between functions based on accessibility of constructor

别来无恙 提交于 2019-12-12 07:57:40
问题 I am writing a class ptr_scope_manager to manage the creation and destruction of pointers in a given scope. I have studied the answers from this question: Private constructor inhibits use of emplace[_back]() to avoid a move And it appears that if I want to manage the creation of an object whose class has a private constructor, my internal std::vector can use push_back but not emplace_back to construct the object. This is because emplace_back uses an internal class to construct the object.