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
51
votes
2 answers

Is the "lazy man's enable_if" legal C++?

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 auto foo(F&& f) -> decltype(f(0),…
Travis Gockel
  • 26,877
  • 14
  • 89
  • 116
50
votes
2 answers

Approaches to function SFINAE in C++

I am using function SFINAE heavily in a project and am not sure if there are any differences between the following two approaches (other than style): #include #include #include template
keith
  • 5,122
  • 3
  • 21
  • 50
49
votes
3 answers

What exactly is the "immediate context" mentioned in the C++11 Standard for which SFINAE applies?

Paragraph 14.8.2/8 of the C++11 Standard specifies the conditions under which a substitution failure shall or shall not result in a "hard" compilation error (thereby causing compilation to fail) or in a "soft" error which would just cause the…
Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
44
votes
4 answers

Why does enable_if_t in template arguments complains about redefinitions?

I have the following case that works using std::enable_if : template::value>::type* = nullptr> void f() { } template
Jean-Michaël Celerier
  • 7,412
  • 3
  • 54
  • 75
44
votes
3 answers

using SFINAE for template class specialisation

suppose I have these declarations template class User; template class Data; and want to implement User<> for T = Data and any class derived from Data but also allow for other specialisations defined…
Walter
  • 44,150
  • 20
  • 113
  • 196
43
votes
3 answers

SFINAE working in return type but not as template parameter

I already used the SFINAE idiom quite a few times and I got used to put my std::enable_if<> in template parameters rather than in return types. However, I came across some trivial case where it didn't work, and I'm not sure why. First of all, here…
Morwenn
  • 21,684
  • 12
  • 93
  • 152
39
votes
2 answers

Detecting constexpr with SFINAE

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…
K-ballo
  • 80,396
  • 20
  • 159
  • 169
38
votes
11 answers

How to write a type trait `is_container` or `is_vector`?

Is it possible to write a type trait whose value is true for all common STL structures (e.g., vector, set, map, ...)? To get started, I'd like to write a type trait that is true for a vector and false otherwise. I tried this, but it doesn't…
Frank
  • 64,140
  • 93
  • 237
  • 324
37
votes
3 answers

Checking for existence of C++ member function, possibly protected

I'm trying to detect whether a class has a particular function (specifically shared_from_this(), which is inherited from std::enable_shared_from_this). To make things more complicated, I need to know whether it has this function…
Azoth
  • 1,652
  • 16
  • 24
36
votes
4 answers

How to detect existence of a class using SFINAE?

Is it possible to detect if a class exists in C++ using SFINAE? If possible then how? Suppose we have a class that is provided only by some versions of a library. I'd like to know if it is possible to use SFINAE to detect whether the class exists or…
vitaut
  • 49,672
  • 25
  • 199
  • 336
35
votes
6 answers

Conditionally disabling a copy constructor

Suppose I'm writing a class template C that holds a T value, so C 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…
Geoff Romer
  • 2,358
  • 1
  • 18
  • 19
34
votes
7 answers

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

I want to do something like template 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…
Jesse Beder
  • 33,081
  • 21
  • 109
  • 146
34
votes
8 answers

check if member exists using enable_if

Here's what I'm trying to do: template struct Model { vector vertices ; #if T has a .normal member void transform( Matrix m ) { each vertex in vertices { vertex.pos = m * vertex.pos ; …
bobobobo
  • 64,917
  • 62
  • 258
  • 363
33
votes
5 answers

How to use sfinae for selecting constructors?

In template meta programming, one can use SFINAE on the return type to choose a certain template member function, i.e. template struct A { int sum() const noexcept { return _sum(); } private: int _data[N]; template
Walter
  • 44,150
  • 20
  • 113
  • 196
33
votes
6 answers

Selecting a member function using different enable_if conditions

I am trying to determine which version of a member function gets called based on the class template parameter. I have tried this: #include #include template struct Point { void MyFunction(typename…
David Doria
  • 9,873
  • 17
  • 85
  • 147