sfinae

Alias templates used in SFINAE lead to a hard error

痴心易碎 提交于 2019-12-02 06:42:38
问题 I want to use an enabler (an alias template of enable_if ), defined in one class template, in another class template. It looks like this: template< ... > using enabler = typename std::enable_if< ... >::type; This works fine for SFINAE. But when I add another alias template in the second class like template< ... > using enabler = typename first_class<..> :: template enabler< ... >; and use this enabler for SFINAE, substitution (correctly) fails but with a hard error for g++4.8.0 and 4.8.1.

SFINAE check if expression compiles and return std::true_type [duplicate]

久未见 提交于 2019-12-02 06:35:58
问题 This question already has answers here : Is it possible to write a template to check for a function's existence? (25 answers) Closed 5 years ago . I want to get std::true_type if the following expression compiles: template<typename T> static constexpr std::true_type check(T*) ?????? std::declval<T>().func_name( std::declval<Args>()... ) // method to check for and std::false_type otherwise which I normally do with template<typename> static constexpr std::false_type check(...); I search

Is this program ill-formed despite SFINAE?

主宰稳场 提交于 2019-12-02 02:29:06
问题 template <typename T> void f() { return 0; // returning value from function returning `void` } int main() { // Not instantiating or calling any f<T>() } In comments to this answer, David asserts that a function template that contains a semantic error and is not instantiated causes a program to be ill-formed: Whether the template is used or not does not matter, the program is ill-formed even with no instantiation but the compiler is not required to diagnose it. Conversely, I am quite sure that

Substitution failure is not an error (SFINAE) for enum

a 夏天 提交于 2019-12-02 01:54:07
问题 Is there a way to use Substitution failure is not an error (SFINAE) for enum? template <typename T> struct Traits { } template <> struct Traits<A> { }; template <> struct Traits<B> { enum { iOption = 1 }; }; template <T> void Do() { // use Traits<T>::iOption }; Then, Do<B>(); works and Do<A>(); fails. However, I can supply a default behavior when iOption does not exist. So I separate out some part of Do to DoOption. template <typename T, bool bOptionExist> void DoOption() { // can't use

How does this SFINAE C++ syntax work?

别来无恙 提交于 2019-12-01 22:19:59
I have just begun dabbling with SFINAE and I am having trouble understanding the syntax behind the most often-used example which appears in various forms, but the idea is to check whether a particular type contains a given member. This particular one is from Wikipedia : template <typename T> struct has_typedef_foobar { typedef char yes[1]; typedef char no[2]; template <typename C> static yes& test(typename C::foobar*); template <typename> static no& test(...); static const bool value = sizeof(test<T>(0)) == sizeof(yes); }; There are a couple of things I don't get about this: What is the

Is out-of-line sfinae on template member functions possible?

℡╲_俬逩灬. 提交于 2019-12-01 21:45:41
Demo A in class declaration of A::foo. struct A { template <typename T> void foo(T a); }; A::foo is now split by sfinae. template <typename T> typename std::enable_if<(sizeof(T) > 4), void>::type A::foo(T a ) { std::cout << "> 4 \n"; } This doesn't work. Is this not allowed? The return type in the declaration must match the definition. struct A { template <typename T> typename std::enable_if<(sizeof(T) > 4), void>::type foo(T a); }; SFINAE cannot be encapsulated as an implementation detail. ( demo ) One way to achieve this is to internally tag-dispatch: #include <utility> #include <iostream>

SFINAE Constructors [duplicate]

这一生的挚爱 提交于 2019-12-01 19:50:39
This question already has an answer here: SFINAE working in return type but not as template parameter 3 answers Template specialization and enable_if problems [duplicate] 1 answer I have been liking SFINAE syntax like this for functions, seems to generally work well! template <class Integer, class = typename std::enable_if<std::is_integral<Integer>::value>::type> T(Integer n) { // ... } But am running into an issue when I want to do this as well in the same class... template <class Float, class = typename std::enable_if<std::is_floating_point<Float>::value>::type> T(Float n) { // ... } Getting

Is it possible to check if a member function is defined for a class even if the member is inherited from an unknown base class

冷暖自知 提交于 2019-12-01 18:48:06
问题 I found similar questions and answers like this one. However, as I tried out, this SFINAE tests only succeeded if the tested member is directly defined in the class being tested. For example the following, class B , D1 print HAS while the other two print NOT HAS . Is there a way to determine that if a class has a member, whether it is defined by itself, or a base class, and the name of the base class is not known in this case. The motivation is that I want to write a generic function that

Expression SFINAE to overload on type of passed function pointer

爷,独闯天下 提交于 2019-12-01 17:53:22
In this example a function is passed to an implicitly instantiated function template. // Function that will be passed as argument int foo() { return 0; } // Function template to call passed function template<typename F> int call(F f) { return f(); } template<typename F, typename A> int call(F f, A a) { return f(a); } int a = call(foo); We can break this code by adding an overload for foo() . int foo(int i) { return 0; } The name " foo " is now ambiguous and the example will no longer compile. This can be made to compile by explicitly providing function pointer type info. int (*func_takes_void)

“anti-SFINAE” enabling an overload if a given expression is *not* well-formed

╄→尐↘猪︶ㄣ 提交于 2019-12-01 17:48:18
问题 It is easy to use SFINAE to hide a particular function overload if a particular expression is not well-formed. But I want to do the opposite, hiding an overload if and only if a given expression is well-formed, and to do so in a very general way. I have a solution that's working in clang 3.5.0 and gcc 5.2.0, but I'm interested in any comments and alternatives. Ideally, there would be a builtin constexpr bool function/macro that would tell us at compile time whether a particular expression is