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
16
votes
1 answer

SFINAE template specialization precedence

#include #include #include template struct trait; template struct trait().begin(), std::declval().end(), void() )> { …
SU3
  • 5,064
  • 3
  • 35
  • 66
16
votes
1 answer

SFINAE on assembly?

Is it possible to use metaprogramming tricks to allow SFINAE on assembly blocks? For example to detect if an instruction like "CPUID" is available on a processor: (this is not valid code, but illustrates what I would like to achieve) // This should…
Vincent
  • 57,703
  • 61
  • 205
  • 388
16
votes
2 answers

void_t and trailing return type with decltype: are they completely interchangeable?

Consider the following, basic example based on void_t: template> struct S: std::false_type {}; template struct S().foo())>>: std::true_type {}; It can be used as it…
skypjack
  • 49,335
  • 19
  • 95
  • 187
16
votes
2 answers

SFINAE not happening with std::underlying_type

Below SFINAE code with variadic templates compiles nicely using clang 3.7.1, C++14: #include #include #include #include enum class Bar : uint8_t { ay, bee, see }; struct S { static void foo() {} //…
Paolo M
  • 12,403
  • 6
  • 52
  • 73
16
votes
3 answers

SFINAE away a copy constructor

Under certain conditions, I'd like to SFINAE away the copy constructor and copy assignment operator of a class template. But if I do so, a default copy constructor and a default assignment operator are generated. The SFINAE is done based on tags I…
user1095108
  • 14,119
  • 9
  • 58
  • 116
16
votes
2 answers

Why is an ellipsis preferred to a variadic template when called with no arguments?

I'm using the following SFINAE pattern to evaluate a predicate on a variadic type list: #include void f(int = 0); // for example template()...))> std::true_type check(T…
ecatmur
  • 152,476
  • 27
  • 293
  • 366
15
votes
4 answers

Can SFINAE detect private access violations?

I wonder whether if i test for some member of a class and the member is private what will sfinae respond? Will it error out hard or will it say ok or will it error out in the sfinae way?
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
15
votes
3 answers

Use a enable_if that does not depend on method's template parameter

I'm trying to use std::enable_if and SFINAE to switch out the implementation of a class template's method based purely on the template parameters of the class. Example: #include template class Foo { …
Lukas Barth
  • 2,734
  • 18
  • 43
15
votes
3 answers

How to detect whether a type is std::tuple or not?

Why I have strange output for this code? How to test for a type in the right way? #include #include #include template struct is_tuple : std::false_type {}; template struct…
15
votes
1 answer

template specialization for derived classes

I have a template class (that I cannot modify), let's call it SomeClass, that I'd like to specialize for classes that derive from a particular class only. Following this answer I was able to do this in gcc 6.3.1, but unfortunately I need to do it in…
eddi
  • 49,088
  • 6
  • 104
  • 155
15
votes
1 answer

Confusion with hard error in SFINAE

With regards to the following code (https://wandbox.org/permlink/nhx4pheijpTF1ohf reproduced below for convenience) #include #include namespace foo_name { template void foo(); template <> void…
Curious
  • 20,870
  • 8
  • 61
  • 146
15
votes
1 answer

Why is the template specialization not chosen?

I wrote the following code: #include #include #include template struct is_incrementable : std::false_type {}; template struct is_incrementable
ellipsis
  • 155
  • 1
  • 5
15
votes
2 answers

C++ 11: overload resolution and SFINAE

I'm learning SFINAE and this is my first attempt to print "YES" only for those types you can output with std::ostream (forget about std::operator<<(std::ostream &, T) for now...): template void f(const T &) { std::cout << "NO" <<…
nodakai
  • 7,773
  • 3
  • 30
  • 60
15
votes
3 answers

Does the standard require std::tuple_size to be SFINAE-friendly?

Edit append: The question title was "do Visual Studio compiler or Clang have incorrect behavior"- but that have been changed. So I add here that clang and gcc compiles it the way I intended, but VS does not. I have the following…
15
votes
3 answers

Execute different functions depending on template parameter disequation

This is definitely a trivial question, but I couldn't figure out how to do this. I have a template function, say template void my_function(). Now, I have two different implementations for my_function, the first should be used if N…
Matteo Monti
  • 8,362
  • 19
  • 68
  • 114