Questions tagged [sfinae]

Substitution failure is not an error. This is a C++ programming technique that allows templates to verify properties about their template parameters, thus allowing different specializations to be used if certain kinds of objects are available.

Substitution failure is not an error (SFINAE) refers to a situation in C++ where an invalid substitution of template parameters during template argument deduction is not in itself an error.

It is thus possible to select from multiple specializations of template functions based on the type of the parameter, because the substitution during template argument deduction does not cause an error in the compilation. The failure in the substitution removes the candidate from the overload list and hence is not considered.

David Vandevoorde first introduced the acronym SFINAE to describe related programming techniques.

1799 questions
24
votes
1 answer

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

#include struct A{}; struct B{}; template struct Foo { typename std::enable_if::value>::type bar() {} typename std::enable_if::value>::type bar() …
Olumide
  • 5,397
  • 10
  • 55
  • 104
24
votes
1 answer

If the address of a function can not be resolved during deduction, is it SFINAE or a compiler error?

In C++0x SFINAE rules have been simplified such that any invalid expression or type that occurs in the "immediate context" of deduction does not result in a compiler error but rather in deduction failure (SFINAE). My question is this: If I take…
Faisal Vali
  • 32,723
  • 8
  • 42
  • 45
23
votes
3 answers

Why do my SFINAE expressions no longer work with GCC 8.2?

I recently upgraded GCC to 8.2, and most of my SFINAE expressions have stopped working. The following is somewhat simplified, but demonstrates the problem: #include #include class Class { public: template < …
zennehoy
  • 6,405
  • 28
  • 55
23
votes
2 answers

Multiple SFINAE class template specialisations using void_t

Are multiple class template specialisations valid, when each is distinct only between patterns involving template parameters in non-deduced contexts? A common example of std::void_t uses it to define a trait which reveals whether a type has a member…
23
votes
3 answers

Partial template function specialization with enable_if: make default implementation

Using C++11's enable_if I want to define several specialized implementations for a function (based on the type of the parameter, say) as well as a default implementation. What is the correct way to define it? The following example does not work as…
Bruno
  • 437
  • 4
  • 8
23
votes
1 answer

Template specialization and enable_if problems

I am running into a problem regarding the appropriate usage of enable_if and template specialization. After modifying the example (for confidentiality reasons), here's a comparable example: I have function called "less" that checks if 1st arg is…
user855
  • 19,048
  • 38
  • 98
  • 162
23
votes
4 answers

can I use SFINAE to selectively define a member variable in a template class?

So what I want to do is to create a template class which may or may not contain a member variable based on the template argument passed in. like following: template class base { foov::type>…
Nowibananatzki
  • 756
  • 1
  • 9
  • 19
23
votes
2 answers

'if' with template parameters or SFINAE is preferred?

Preferred is this: template bool isNotZero(const T &a) { if (std::is_floating_point::value) return abs(a) > std::numeric_limits::epsilon(); else return a; } Or this:? template
Chameleon
  • 1,804
  • 2
  • 15
  • 21
23
votes
4 answers

Check if type is hashable

I would like to make a type trait for checking if a particular type is hashable using the default instantiations of the standard library's unordered containers, thus if it has a valid specialization for std::hash. I think this would be a very useful…
Christian Rau
  • 45,360
  • 10
  • 108
  • 185
22
votes
3 answers

SFINAE works with deduction but fails with substitution

Consider the following MCVE struct A {}; template void test(T, T) { } template class Wrapper { using type = typename T::type; }; template void test(Wrapper, Wrapper) { } int main() { A a, b; test(a,…
rustyx
  • 80,671
  • 25
  • 200
  • 267
22
votes
5 answers

SFINAE works differently in cases of type and non-type template parameters

Why does this code work: template< typename T, std::enable_if_t::value, T>* = nullptr> void Add(T) {} template< typename T, std::enable_if_t::value, T>* = nullptr> void Add(T) {} and can…
undermind
  • 1,779
  • 13
  • 33
22
votes
3 answers

Using SFINAE to check if the type is complete or not

Is it possible to check with SFINAE if the type is completely defined? E.g. template struct hash; template <> struct hash {}; // is_defined_hash_type definition... enum Enum { A, B, C, D }; static_assert ( …
Nikki Chumakov
  • 1,215
  • 8
  • 18
21
votes
3 answers

SFINAE with invalid function-type or array-type parameters?

Please consider this code: template char (&f(T[1]))[1]; template char (&f(...))[2]; int main() { char c[sizeof(f(0)) == 2]; } I expected it doing SFINAE and chosing the second overload, since substitution of T into…
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
21
votes
1 answer

Why void_t doesnt work in SFINAE but enable_if does

I was trying to understand how SFINAE works and I was experimenting with this code #include struct One { using x = int; }; struct Two { using y = int; }; template * = nullptr> void…
user4386938
21
votes
4 answers

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

In MSVC2017 this works fine, both static_asserts are NOT triggered as expected: template struct do_have_size { template ().size())> static std::true_type check(T); static std::false_type…
Starl1ght
  • 4,422
  • 1
  • 21
  • 49