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.

1580 questions
539
votes
31 answers

Templated check for the existence of a class member function?

Is it possible to write a template that changes behavior depending on if a certain member function is defined on a class? Here's a simple example of what I would want to write: template std::string optionalToString(T* obj) { if…
andy
  • 16,174
  • 9
  • 28
  • 27
288
votes
28 answers

How can I add reflection to a C++ application?

I'd like to be able to introspect a C++ class for its name, contents (i.e. members and their types) etc. I'm talking native C++ here, not managed C++, which has reflection. I realise C++ supplies some limited information using RTTI. Which additional…
Nick
  • 25,635
  • 12
  • 56
  • 71
172
votes
4 answers

Why should I avoid std::enable_if in function signatures

Scott Meyers posted content and status of his next book EC++11. He wrote that one item in the book could be "Avoid std::enable_if in function signatures". std::enable_if can be used as a function argument, as a return type or as a class template or…
hansmaad
  • 16,551
  • 7
  • 46
  • 89
161
votes
2 answers

How does `void_t` work

I watched Walter Brown's talk at Cppcon14 about modern template programming (Part I, Part II) where he presented his void_t SFINAE technique. Example: Given a simple variable template that evaluates to void if all template arguments are well…
nonsensation
  • 3,317
  • 5
  • 26
  • 39
147
votes
17 answers

Check if a class has a member function of a given signature

I'm asking for a template trick to detect if a class has a specific member function of a given signature. The problem is similar to the one cited here http://www.gotw.ca/gotw/071.htm but not the same: in the item of Sutter's book he answered to the…
ugasoft
  • 3,382
  • 7
  • 25
  • 23
134
votes
9 answers

C++ SFINAE examples?

I want to get into more template meta-programming. I know that SFINAE stands for "substitution failure is not an error." But can someone show me a good use for SFINAE?
rlbond
  • 59,991
  • 50
  • 166
  • 218
87
votes
2 answers

How is std::is_function implemented?

How is the following an implementation for std::is_function? template struct is_function : std::integral_constant< bool, !std::is_const::value && !std::is_reference::value > {}; (from CPP Reference) Seems to me, an int…
Rian Quinn
  • 1,528
  • 9
  • 17
74
votes
10 answers

How to detect whether there is a specific member variable in class?

For creating algorithm template function I need to know whether x or X (and y or Y) in class that is template argument. It may by useful when using my function for MFC CPoint class or GDI+ PointF class or some others. All of them use different x in…
Kirill V. Lyadvinsky
  • 89,955
  • 22
  • 127
  • 208
57
votes
1 answer

What is "Expression SFINAE"?

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…
xmllmx
  • 33,981
  • 13
  • 121
  • 269
56
votes
3 answers

Select class constructor using enable_if

Consider following code: #include #include template struct A { int val = 0; template ::type> A(int n) : val(n) {}; A(...) { } /* ... */ }; struct…
tomas789
  • 1,160
  • 1
  • 8
  • 20
55
votes
13 answers

How to check whether operator== exists?

I am trying to create an example, which would check the existence of the operator== (member or, non-member function). To check whether a class has a member operator== is easy, but how to check whether it has a non-member operator==? This is what I…
BЈовић
  • 57,268
  • 38
  • 158
  • 253
53
votes
1 answer

What does the 'void()' in 'auto f(params) -> decltype(..., void())' do?

I found code here that looked something like this: auto f(T& t, size_t n) -> decltype(t.reserve(n), void()) { .. } In all the documentation I read I was told that decltype is signed as: decltype( entity ) or decltype( expression ) And there is…
template boy
  • 9,266
  • 4
  • 51
  • 91
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
  • 24,743
  • 10
  • 80
  • 105
49
votes
2 answers

What is decltype with two arguments?

Edit, in order to avoid confusion: decltype does not accept two arguments. See answers. The following two structs can be used to check for the existance of a member function on a type T during compile-time: // Non-templated helper struct: struct…
leemes
  • 42,229
  • 18
  • 115
  • 172
48
votes
5 answers

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

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?
Jim
  • 483
  • 5
  • 4
1
2 3
99 100