sfinae

SFINAE with C++14 return type deduction

你说的曾经没有我的故事 提交于 2019-12-17 19:44:26
问题 Thanks to C++14, we'll soon be able to curtail verbose trailing return types; such as the generic min example from David Abrahams 2011 post: template <typename T, typename U> auto min(T x, U y) -> typename std::remove_reference< decltype(x < y ? x : y) >::type { return x < y ? x : y; } Under C++14 the return type can be omitted, and min can be written as: template <typename T, typename U> auto min(T x, U y) { return x < y ? x : y; } This is a simple example, however return type deduction is

Conditionally disabling a copy constructor

你。 提交于 2019-12-17 17:58:07
问题 Suppose I'm writing a class template C<T> that holds a T value, so C<T> can be copyable only if T is copyable. Normally, when a template might or might not support a certain operation, you just define the operation, and it's up to your callers to avoid calling it when it's not safe: template <typename T> class C { private: T t; public: C(const C& rhs); C(C&& rhs); // other stuff }; However, this creates problems in the case of a copy constructor, because is_copy_constructible<C<T>> will be

Select class constructor using enable_if

十年热恋 提交于 2019-12-17 08:32:31
问题 Consider following code: #include <iostream> #include <type_traits> template <typename T> struct A { int val = 0; template <class = typename std::enable_if<T::value>::type> A(int n) : val(n) {}; A(...) { } /* ... */ }; struct YES { constexpr static bool value = true; }; struct NO { constexpr static bool value = false; }; int main() { A<YES> y(10); A<NO> n; std::cout << "YES: " << y.val << std::endl << "NO: " << n.val << std::endl; } I want to selectively define constructor A::A(int) only for

Why doesn't SFINAE (enable_if) work for member functions of a class template?

放肆的年华 提交于 2019-12-17 07:29:38
问题 #include <type_traits> struct A{}; struct B{}; template <typename T> struct Foo { typename std::enable_if<std::is_same<T, A>::value>::type bar() {} typename std::enable_if<std::is_same<T, B>::value>::type bar() {} }; Error message: 14:5: error: 'typename std::enable_if<std::is_same<T, B>::value>::type Foo<T>::bar()' cannot be overloaded 10:5: error: with 'typename std::enable_if<std::is_same<T, A>::value>::type Foo<T>::bar()' Source on cpp.sh. I thought both typename std::enable_if<std::is

Explain C++ SFINAE to a non-C++ programmer

只谈情不闲聊 提交于 2019-12-17 05:37:27
问题 What is SFINAE in C++? Can you please explain it in words understandable to a programmer who is not versed in C++? Also, what concept in a language like Python does SFINAE correspond to? 回答1: Warning: this is a really long explanation, but hopefully it really explains not only what SFINAE does, but gives some idea of when and why you might use it. Okay, to explain this we probably need to back up and explain templates a bit. As we all know, Python uses what's commonly referred to as duck

Explain C++ SFINAE to a non-C++ programmer

爱⌒轻易说出口 提交于 2019-12-17 05:37:26
问题 What is SFINAE in C++? Can you please explain it in words understandable to a programmer who is not versed in C++? Also, what concept in a language like Python does SFINAE correspond to? 回答1: Warning: this is a really long explanation, but hopefully it really explains not only what SFINAE does, but gives some idea of when and why you might use it. Okay, to explain this we probably need to back up and explain templates a bit. As we all know, Python uses what's commonly referred to as duck

Detecting constexpr with SFINAE

余生长醉 提交于 2019-12-17 05:02:10
问题 I'm working on upgrading some C++ code to take advantage of the new functionality in C++11. I have a trait class with a few functions returning fundamental types which would most of the time, but not always, return a constant expression. I would like to do different things based on whether the function is constexpr or not. I came up with the following approach: template<typename Trait> struct test { template<int Value = Trait::f()> static std::true_type do_call(int){ return std::true_type();

What is “Expression SFINAE”?

你离开我真会死。 提交于 2019-12-17 02:09:38
问题 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 {};

Why this SFINAE snippet is not working in g++, but working in MSVC?

风格不统一 提交于 2019-12-13 12:52:25
问题 In MSVC2017 this works fine, both static_asserts are NOT triggered as expected: template <typename T> struct do_have_size { template <typename = decltype(std::declval<T>().size())> static std::true_type check(T); static std::false_type check(...); using type = decltype(check(std::declval<T>())); }; int main() { using TR = typename do_have_size<std::vector<int>>::type; using FL = typename do_have_size<int>::type; static_assert(std::is_same<TR, std::true_type>::value, "TRUE"); static_assert(std

error: functional cast to array type while trying to detect if std::cout << t; is valid

﹥>﹥吖頭↗ 提交于 2019-12-13 10:05:45
问题 Triggered by a comment to this answer I would like to write (in C++11) a template <typename T> struct has_out_op { static const bool value = ???; } to dis/enable a member function depending on std::cout << t; being valid for some T t . I came this far... #include <iostream> struct can_convert_to_base{}; // but does not when there is a better match struct base {base(can_convert_to_base);}; template <typename T> auto test(const T& t,can_convert_to_base) -> decltype( std::cout << t); template