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
32
votes
4 answers

SFINAE to check for inherited member functions

Using SFINAE, i can detect wether a given class has a certain member function. But what if i want to test for inherited member functions? The following does not work in VC8 and GCC4 (i.e. detects that A has a member function foo(), but not that B…
Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
31
votes
2 answers

What does it mean when one says something is SFINAE-friendly?

I can't clearly get the grasp of what it means when one mentions that a particular function, struct or ... is SFINAE-friendly. Would someone please explain it?
Mehrdad
  • 1,186
  • 7
  • 15
31
votes
5 answers

Why compile error with enable_if

Why this does not compile with gcc48 and clang32? #include template struct S { template typename std::enable_if::type f(T t) {return 1;}; template typename…
Leonid Volnitsky
  • 8,854
  • 5
  • 38
  • 53
30
votes
8 answers

Metaprograming: Failure of Function Definition Defines a Separate Function

In this answer I define a template based on the type's is_arithmetic property: template enable_if_t::value, string> stringify(T t){ return to_string(t); } template enable_if_t::value,…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
30
votes
2 answers

How to detect if a method is virtual?

I tried to make a traits to find if a method is virtual: (https://ideone.com/9pfaCZ) // Several structs which should fail depending if T::f is virtual or not. template struct Dvf : T { void f() final; }; template struct Dvo…
Jarod42
  • 203,559
  • 14
  • 181
  • 302
29
votes
4 answers

When to use `static_assert` instead of SFINAE?

I have been using (and seen used) static_assert to flag undesired values of template parameter values. However, for all cases I came across it seems better and more elegant to disable those undesired values via SFINAE. For example: template
Walter
  • 44,150
  • 20
  • 113
  • 196
28
votes
2 answers

Using alias templates for sfinae: does the language allow it?

I have just discovered the following technique. It looks very close to one of proposed concepts syntax, works perfectly on Clang, GCC and MSVC. template
Nikita Kniazev
  • 3,728
  • 2
  • 16
  • 30
28
votes
6 answers

Assert that code does NOT compile

In short: How to write a test, that checks that my class is not copyable or copy-assignable, but is only moveable and move-assignable? In general: How to write a test, that makes sure that a specific code does not compile? Like this: // Movable, but…
Mikhail
  • 20,685
  • 7
  • 70
  • 146
27
votes
2 answers

SFINAE: static_assert vs std::enable_if

Are there any disadvantages in the following (suggested!) syntax? template< typename T > void f() static_assert(std::is_same< T, int >::value) { ; } instead of SFINAE (that looks like a crutch): template< typename T, typename = typename…
Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169
27
votes
1 answer

How to avoid this sentence is false in a template SFINAE?

So I want to write an automatic !=: template bool operator!=(U&& u, T&& t) { return !( std::forward(u) == std::forward(t) ); } but that is impolite1. So I write // T() == U() is valid? template
Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
26
votes
3 answers

What trait / concept can guarantee memsetting an object is well defined?

Let's say I have defined a zero_initialize() function: template T zero_initialize() { T result; std::memset(&result, 0, sizeof(result)); return result; } // usage: auto data = zero_initialize(); Calling zero_initialize()…
YSC
  • 38,212
  • 9
  • 96
  • 149
25
votes
1 answer

Why using SFINAE to find if a method exists fails with std::vector::begin

I'm looking for a way to detect if a template class has the methods begin, end and resize. I tried a modified version of this answer: #include #include // SFINAE test template class has_method { typedef char…
TommyD
  • 731
  • 8
  • 14
25
votes
3 answers

Partial specialization of a method in a templated class

Given: struct A { virtual bool what() = 0; }; template struct B : public A { virtual bool what(); }; I want to partially specialize what like: template bool B::what() { return…
David
  • 27,652
  • 18
  • 89
  • 138
24
votes
3 answers

static_assert if expressions is constexpr

I want to create a class template template class X { // here I'll use T::value (among other things) }; T::value will often be a constexpr static variable, but not always. T::value has to be positive value, so I want to let people know…
RiaD
  • 46,822
  • 11
  • 79
  • 123
24
votes
5 answers

What's the right way to fix this template resolution ambiguity?

Suppose I've written: template ::value>> void foo() { std::cout << "T is integral." << std::endl; } template void foo() { std::cout << "Any T." << std::endl; } int main() {…
einpoklum
  • 118,144
  • 57
  • 340
  • 684