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
21
votes
0 answers

What is the reason for providing a default value of zero for SFINAE testers?

I noted that much of boost and libc++/libstdc++ explicitly provide a default value of zero for SFINAE in code like // libc++ http://llvm.org/svn/llvm-project/libcxx/trunk/include/memory namespace __has_pointer_type_imp { template
graphitemaster
  • 3,483
  • 4
  • 19
  • 14
20
votes
2 answers

SFINAE on functions with default parameters - free function vs operator()

I was playing around with this answer to investigate how it handles functions with default parameters. To my surprise, the results are different for free functions and operator(): template auto func(F f) -> decltype(f(42)) { int a =…
Max Langhof
  • 23,383
  • 5
  • 39
  • 72
20
votes
2 answers

How to decide if a template specialization exist

I would like to check if a certain template specialization exist or not, where the general case is not defined. Given: template struct A; // general definition not defined template <> struct A {}; // specialization defined for…
Fabio
  • 2,105
  • 16
  • 26
19
votes
3 answers

SFINAE: "enable_if cannot be used to disable this declaration"

Why can I not use enable_if in the following context? I'd like to detect whether my templated object has the member function notify_exit template class MyQueue { public: auto notify_exit() -> typename std::enable_if< …
Matt
  • 1,928
  • 24
  • 44
19
votes
1 answer

Accessing member type with `if constexpr` inside generic lambda requires both branches to be well-formed - gcc vs clang

Consider two structs with different member type aliases: struct foo { using x = int; }; struct bar { using y = float; }; Given a T in a template context, I want to get either T::x or T::y depending on what T is: template auto s() { …
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
18
votes
3 answers

declval expression (for SFINAE) with std::ostream

I'm trying to create a type traits class to determine if a particular type T can be streamed via the << operator of an std::ostream. I'm using a straightforward SFINAE technique. Ultimately, the expression I attempt to evaluate for substitution…
Siler
  • 8,976
  • 11
  • 64
  • 124
18
votes
2 answers

void_t fails on Visual Studio 2015

I don't understand why the following test always fails with Visual Studio 2015 (the static_assert triggers): #include using namespace std; template using try_assign = decltype(declval() = declval
17
votes
2 answers

template overloading and SFINAE working only with functions but not classes

can someone explain why the compiler accepts only this code template::type =0> void a_function(){} template
Lorenzo Pistone
  • 5,028
  • 3
  • 34
  • 65
17
votes
4 answers

Making `std::get` play nice with SFINAE

std::get does not seem to be SFINAE-friendly, as shown by the following test case: template auto foo(C &c) -> decltype(std::get(c)) { return std::get(c); } template void foo(...) { } int main() { …
Quentin
  • 62,093
  • 7
  • 131
  • 191
17
votes
2 answers

Should the following code compile according to C++ standard?

#include template struct C; template using first = T1; template struct C::value>>> { }; int main () { } Results of compilation by…
Predelnik
  • 5,066
  • 2
  • 24
  • 36
17
votes
1 answer

Private member access in template substitution and SFINAE

class A { int a; }; template class test {}; template class test {}; int main() { test a; } The code above compiles without error on clang version 3.8.0-2ubuntu4…
user4407569
17
votes
3 answers

SFINAE fallback if division operator is not implemented

I want to write a function which perform a division between two arguments a and b of different type, using the expression a/b if the division operator is defined, or fall back in the a * (1/b) if there is no such operator. This is what i thought of,…
pqnet
  • 6,070
  • 1
  • 30
  • 51
16
votes
1 answer

SFINAE tried with bool gives compiler error: "template argument ‘T::value’ involves template parameter"

I tried to implement an SFINAE using bool (unlike popular void_ trick): template struct Resolve { static const bool value = false; }; template struct Resolve { static const…
iammilind
  • 68,093
  • 33
  • 169
  • 336
16
votes
3 answers

What is wrong with my application of SFINAE when trying to implement a type trait?

I needed a type trait that decays enums to their underlying type, and works the same as decay_t for all other types. I've written the following code, and apparently this is not how SFINAE works. But it is how I thought it should work, so what…
Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335
16
votes
5 answers

Detect operator support with decltype/SFINAE

A (somewhat) outdated article explores ways to use decltype along with SFINAE to detect if a type supports certain operators, such as == or <. Here's example code to detect if a class supports the < operator: template struct…
Channel72
  • 24,139
  • 32
  • 108
  • 180