sfinae

SFINAE to have a class member only if possible

混江龙づ霸主 提交于 2019-12-08 17:25:23
问题 I have made a type-safe ID class, but now I want to support operator++ if the underlying type also has it. From this and this answears I have come up with 2 alternatives, but they both fail when instantiated with AId: template<typename T, typename TID = unsigned int> struct AId { typedef AId<T, TID> type; typedef T handled_type; typedef TID value_type; private: value_type id; template<typename _T> struct IsIncrementable { template<typename _U> using rm_ref = typename std::remove_reference<_U>

Specializing class with SFINAE if a parameter pack is needed

一个人想着一个人 提交于 2019-12-08 10:10:29
问题 As I got a perfect answer for the question: Specializing class with SFINAE For completeness I insert the correct solution as example here again: 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, void >::type> { public: X() { std::cout << "First" << std::endl; } }; template < typename T > class X<T,

C++: Manual disambiguation of partial specialization (with SFINAE)

我与影子孤独终老i 提交于 2019-12-08 09:52:32
问题 I am implementing a generic class, which should behave differently for different sets of types (not only for different discrete types). The goal is to serialize objects of different types to send them over custom protocol (but it is more educational task rather than something pragmatical; I am a student that is interested in distributed computing). For example, I need to send floats and integers differently. I also want to have a default handler of other POD types. But I need to override the

SFINAE for class member function (one compiles the other not)

旧时模样 提交于 2019-12-08 08:14:29
问题 Why is class A compiling and class B not compiling, where the compiler complains about having two declarations, are not both relying on SFINAE? Both should actually use template type deduction when using foo ? So the question really is, whats the subtle different in these two version and why is class A successfully using sfinae...? Class A uses a value-defaulted (zero) annonymous (not necessary) non-type template parameter Class B uses a type-defaulted (with enable_if) annonymous type

How To Convert Templated Function Overloads to Partial-Specialized Templated Class Static Methods?

三世轮回 提交于 2019-12-08 05:40:02
问题 I have several functions that I want to specialize based on type qualities, such as "character, signed-integer, unsigned-integer, floating-point, pointer"; using type_traits seems like the way to do this, and have code similar to the to the following: #include <tr1/type_traits> #include <iostream> template<bool, typename _Tp = void> struct enable_if { }; template<typename _Tp> struct enable_if<true, _Tp> { typedef _Tp type; }; template< typename T > inline void foo_impl( typename enable_if<

Template function for detecting pointer like (dereferencable) types fails for actual pointer types

拥有回忆 提交于 2019-12-08 05:38:23
问题 I am trying to write a mechanism to detect if a type is a pointer like type. By that I mean it is dereferencable through operator*() and operator->() . I have three different structs that are specialized accordingly: is_pointer_like_dereferencable which checks for operator*() is_pointer_like_arrow_dereferencable which checks for operator->() is_pointer_like which simply combines 1 & 2 I added specializations for non-templated types like int, int*, ... and for templated types like std::vector<

SFINAE Duplicate constructor declaration

荒凉一梦 提交于 2019-12-08 03:49:47
问题 I want to create constructors for a class in a way that the compiler trivially create new instances of it when needed. Here's an example. class C { public: C(int) {}; // int constructor }; If I then declare a function: void F(C _c) {}; I can call this one with an int and have the construction of C handled by the compiler: F(0); // works What I want to do is to achieve the same thing, but with lambdas as parameters, a few examples: F([]() {}); //A F([](int) {}); //B F([](int)->int { return 0;

Template argument deduction failed, SFINAE

此生再无相见时 提交于 2019-12-07 21:13:53
问题 When I compile this code: #include <type_traits> template <typename T> void do_stuff(std::enable_if_t<std::is_integral<T>::value, T> &t) {} template <typename T> void do_stuff(std::enable_if_t<std::is_class<T>::value, T> &t) {} int main() { int i = 1; do_stuff(i); return 0; } GCC says: 37325975.cpp: In function ‘int main()’: 37325975.cpp:11:15: error: no matching function for call to ‘do_stuff(int&)’ do_stuff(i); ^ 37325975.cpp:4:6: note: candidate: template<class T> void do_stuff(std::enable

How to use SFINAE to select constructor from multiple options in C++11

限于喜欢 提交于 2019-12-07 18:36:37
问题 My question is an extension of this question: How to use sfinae for selecting constructors? In the previous question, the asker just wanted to selectively enable a single constructor. I would like to change the behaviour of the constructor depending on whether the class template parameter type is default-constructible - the best way that I can come up with to do this is to have two constructors with the same usage such that exactly one is enabled for every instantiation. My case is also

C++11 std equivalent of Boost has_dereference

徘徊边缘 提交于 2019-12-07 18:06:59
问题 Many of Boost's SFINAE helpers have appeared in the std library with C++11, but has_dereference doesn't seem to have. Other than this feature, I've managed to eliminate a Boost dependency from my package, and I'd like to get rid of it entirely, so how best to get the same effect using just C++11 std features? 回答1: The easiest way to check if a class has some function with no external dependencies is generally with the void_t idiom. // Define this once in your project somewhere accessible