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
622
votes
34 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
  • 18,005
  • 9
  • 30
  • 27
323
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
  • 27,566
  • 12
  • 60
  • 72
180
votes
3 answers

How do we use void_t for SFINAE?

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,627
  • 6
  • 28
  • 41
180
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
  • 18,417
  • 9
  • 53
  • 94
173
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,778
  • 7
  • 27
  • 23
171
votes
10 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
  • 65,341
  • 56
  • 178
  • 228
90
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,766
  • 12
  • 22
82
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
  • 97,037
  • 24
  • 136
  • 212
67
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,280
  • 1
  • 9
  • 21
64
votes
14 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Јовић
  • 62,405
  • 41
  • 173
  • 273
58
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
  • 39,765
  • 26
  • 162
  • 323
57
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
  • 44,967
  • 21
  • 135
  • 183
57
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
  • 10,230
  • 8
  • 61
  • 97
52
votes
3 answers

Why do constant expressions have an exclusion for undefined behavior?

I was researching what is allowed in a core constant expression*, which is covered in section 5.19 Constant expressions paragraph 2 of the draft C++ standard which says: A conditional-expression is a core constant expression unless it involves one…
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
51
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
  • 513
  • 5
  • 4
1
2 3
99 100