sfinae

Template specialisation with default argument [duplicate]

≯℡__Kan透↙ 提交于 2019-12-06 05:03:01
This question already has answers here : How does `void_t` work (2 answers) Closed last year . I have a program that is as follows. There is a base template struct X and a partial specialisation with SFINAE. template <typename T, typename U = void> struct X{ X() { std::cout << "in 1" << std::endl; }; }; template <typename T> struct X< T, std::enable_if_t<std::is_integral_v<T>> > { X() { std::cout << "in 2" << std::endl; }; }; int main() { X<int> x; } When running the program in 2 is printed. Why is it that the second specialization is chosen over the first since both of them effectively

Template argument deduction failed, SFINAE

若如初见. 提交于 2019-12-06 04:49:15
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_if_t<std::is_integral<_Tp>::value, T>&) void do_stuff(std::enable_if_t<std::is_integral<T>::value, T>

SFINAE - Trying to determine if template type has member function with 'variable' return type

时光怂恿深爱的人放手 提交于 2019-12-06 03:55:39
问题 Having trouble with SFINAE. I need to be able to determine if a Type has a member function operator-> defined regardless of its return type. Example follows. This class in the tester. It defines operator->() with a return type of X*. I thus will not know what 'X' is to hardcode it everywhere. template <class X> class PointerX { ... X* operator->() const; ... } This class tries to determines if the passed in T has a method operator-> defined; regardless of what operator-> return type is.

Boost MPL: Call a (member) function only if it exists

拈花ヽ惹草 提交于 2019-12-06 02:44:25
问题 I have a class A that has a template parameter T. There are use cases where the class T offers a function func1() and there are use cases where T doesn't offer it. A function f() in A should call func1(), iff it exists. I think this should be possible with boost mpl, but I don't know how. Here some pseudo code: template<class T> class A { void f(T param) { if(T::func1 is an existing function) param.func1(); } }; Even better would be an else-case: template<class T> class A { void f(T param) {

Choose template function based on existence of member

泄露秘密 提交于 2019-12-06 01:43:21
问题 Let's say you have these two classes: class A { public: int a; int b; } class B { public: int a; int b; } class C { public: float a1; float b1; } enum class Side { A, B }; I want a template function which takes a side and a T , and depending on the T , returns a reference to " T.a " or " T.b " if the class has a member T::a , or a reference to " T.a1 " or " T.b1 " if the class has a member T::a1 . My starting point is: template<typename T> auto &GetBySide(const Side &side, const T &twoSided)

specialize a template class constructor

China☆狼群 提交于 2019-12-05 21:33:23
I want to specialize a template class constructor: If type is int default value is 50 and -50 . and if it's float default should be 0.5 and -0.5 . My code is : #include <iostream> #include <limits> #include <type_traits> template<typename T> class Foo{ public: template<typename = typename std::enable_if< std::is_integral<T>::value&& !std::is_floating_point<T>::value>::type> Foo(T value1 = 50, T value2 = -50) :value1_(value1), value2_(value2){} template<typename = typename std::enable_if< std::is_floating_point<T>::value>::type> Foo(T value1 = 0.5, T value2 = -0.5, void* dummy = 0) : value1_

Why does order of declaring function changes overload chosen by SFINAE?

十年热恋 提交于 2019-12-05 20:03:32
This question is related to this answer . In this example SFINAE uses variable template has_literal_x specialization instead of the base template: struct A { }; A operator"" _x(char const*) { return {}; } template<typename T, typename S, typename = void> constexpr bool has_literal_x = false; template<typename T, typename S> constexpr bool has_literal_x<T, S, std::enable_if_t< std::is_same< decltype(operator""_x(std::declval<S>())), T >::value > > = true; int main() { std::cout << has_literal_x<A, char const*> << std::endl; // 1 } And here it uses the base template: template<typename T,

C++11 std equivalent of Boost has_dereference

限于喜欢 提交于 2019-12-05 19:55:04
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? Nir Friedman 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 template <class ... T> using void_t = void; The trick then is always the same; you define a class

Why does SFINAE not apply to this?

拥有回忆 提交于 2019-12-05 17:42:26
I'm writing some simple point code while trying out Visual Studio 10 (Beta 2), and I've hit this code where I would expect SFINAE to kick in, but it seems not to: template<typename T> struct point { T x, y; point(T x, T y) : x(x), y(y) {} }; template<typename T, typename U> struct op_div { typedef decltype(T() / U()) type; }; template<typename T, typename U> point<typename op_div<T, U>::type> operator/(point<T> const& l, point<U> const& r) { return point<typename op_div<T, U>::type>(l.x / r.x, l.y / r.y); } template<typename T, typename U> point<typename op_div<T, U>::type> operator/(point<T>

why SFINAE (enable_if) works from inside class definition but not from outside

爷,独闯天下 提交于 2019-12-05 11:36:00
Very weird problem I've been struggling with for the past few hours (after solving 5-6 other issues with SFINAE as I'm new to it). Basically in the following code I want to have f() working for all possible template instantiations, but have g() available only when N == 2 : #include <type_traits> #include <iostream> template<typename T, int N> class A { public: void f(void); void g(void); }; template<typename T, int N> inline void A<T, N>::f() { std::cout << "f()\n"; } template<typename T, int N, typename std::enable_if<N == 2, void>::type* = nullptr> inline void A<T, N>::g() { std::cout << "g(