sfinae

What does it mean when one says something is SFINAE-friendly?

廉价感情. 提交于 2019-12-03 04:17:00
问题 I can't clearly get the grasp of what it means when one mentions that a particular function, struct or ... is SFINAE-friendly . Would someone please explain it? 回答1: When it allows substitution failure without hard error (as static_assert ). for example template <typename T> void call_f(const T& t) { t.f(); } The function is declared for all T , even those with don't have f , so you cannot do SFINAE on call_f<WithoutF> as the method does exist. (Demo of non compiling code). With following

SFINAE: detect if class has free function

独自空忆成欢 提交于 2019-12-03 03:26:28
Is there a way, using SFINAE, to detect whether a free function is overloaded for a given class? Basically, I’ve got the following solution: struct has_no_f { }; struct has_f { }; void f(has_f const& x) { } template <typename T> enable_if<has_function<T, f>::value, int>::type call(T const&) { std::cout << "has f" << std::endl; } template <typename T> disable_if<has_function<T, f>::value, int>::type call(T const&) { std::cout << "has no f" << std::endl; } int main() { call(has_no_f()); // "has no f" call(has_f()); // "has f" } Simply overloading call doesn’t work since there are actually a lot

SFINAE compiler troubles

做~自己de王妃 提交于 2019-12-03 03:20:26
The following code of mine should detect whether T has begin and end methods: template <typename T> struct is_container { template <typename U, typename U::const_iterator (U::*)() const, typename U::const_iterator (U::*)() const> struct sfinae {}; template <typename U> static char test(sfinae<U, &U::begin, &U::end>*); template <typename U> static long test(...); enum { value = (1 == sizeof test<T>(0)) }; }; And here is some test code: #include <iostream> #include <vector> #include <list> #include <set> #include <map> int main() { std::cout << is_container<std::vector<std::string> >::value << '

How to call a templated function if it exists, and something else otherwise?

懵懂的女人 提交于 2019-12-03 01:39:55
问题 I want to do something like template <typename T> void foo(const T& t) { IF bar(t) would compile bar(t); ELSE baz(t); } I thought that something using enable_if would do the job here, splitting up foo into two pieces, but I can't seem to work out the details. What's the simplest way of achieving this? 回答1: There are two lookups that are done for the name bar . One is the unqualified lookup at the definition context of foo . The other is argument dependent lookup at each instantiation context

How do I determine if a type is callable with only const references?

亡梦爱人 提交于 2019-12-02 23:59:30
I want to write a C++ metafunction is_callable<F, Arg> that defines value to be true , if and only if the type F has the function call operator of the form SomeReturnType operator()(const Arg &) . For example, in the following case struct foo { void operator(const int &) {} }; I want is_callable<foo, int &> to be false and is_callable<foo, const int &> to be true . This is what I have so far : #include <memory> #include <iostream> template<typename F, typename Arg> struct is_callable { private: template<typename> static char (&test(...))[2]; template<unsigned> struct helper { typedef void

Using alias templates for sfinae: does the language allow it?

≡放荡痞女 提交于 2019-12-02 18:57:57
I have just discovered the following technique. It looks very close to one of proposed concepts syntax, works perfectly on Clang, GCC and MSVC. template <typename T, typename = typename std::enable_if<std::is_rvalue_reference<T&&>::value>::type> using require_rvalue = T&&; template <typename T> void foo(require_rvalue<T> val); I tried to find it with search requests like "sfinae in type alias" and got nothing. Is there a name for this technique and does the language actually allows it? The full example: #include <type_traits> template <typename T, typename = typename std::enable_if<std::is

What does it mean when one says something is SFINAE-friendly?

╄→尐↘猪︶ㄣ 提交于 2019-12-02 17:34:44
I can't clearly get the grasp of what it means when one mentions that a particular function, struct or ... is SFINAE-friendly . Would someone please explain it? When it allows substitution failure without hard error (as static_assert ). for example template <typename T> void call_f(const T& t) { t.f(); } The function is declared for all T , even those with don't have f , so you cannot do SFINAE on call_f<WithoutF> as the method does exist. ( Demo of non compiling code). With following change: template <typename T> auto call_f(const T& t) ->decltype(t.f(), void()) { t.f(); } The method exists

Is the “lazy man's enable_if” legal C++?

无人久伴 提交于 2019-12-02 16:57:47
I frequently use a technique I call the "lazy man's enable_if ," where I use decltype and the comma operator to enable a function based on some template input. Here is a small example: template <typename F> auto foo(F&& f) -> decltype(f(0), void()) { std::cout << "1" << std::endl; } template <typename F> auto foo(F&& f) -> decltype(f(0, 1), void()) { std::cout << "2" << std::endl; } With --std=c++11 , g++ 4.7+ and Clang 3.5+ happily compile that bit of code (and it works as I would expect). However, when using MSVC 14 CTP5, I get this error complaining of foo already being defined: Error error

When to use `static_assert` instead of SFINAE?

情到浓时终转凉″ 提交于 2019-12-02 16:19:16
I have been using (and seen used) static_assert to flag undesired values of template parameter values. However, for all cases I came across it seems better and more elegant to disable those undesired values via SFINAE. For example template<typename T, class = std::enable_if<std::is_floating_point<T>::value>::type> struct Foo { ... }; instead of template<typename T> struct Foo { static_assert(std::is_floating_point<T>::value, "Foo<T>: T must be floating point :-("); ... }; So my question: when to use static_assert instead of SFINAE and why? EDIT I think what I've learned so far is the following

How to call a templated function if it exists, and something else otherwise?

一世执手 提交于 2019-12-02 15:10:20
I want to do something like template <typename T> void foo(const T& t) { IF bar(t) would compile bar(t); ELSE baz(t); } I thought that something using enable_if would do the job here, splitting up foo into two pieces, but I can't seem to work out the details. What's the simplest way of achieving this? There are two lookups that are done for the name bar . One is the unqualified lookup at the definition context of foo . The other is argument dependent lookup at each instantiation context (but the result of the lookup at each instantiation context is not allowed to change behavior between two