By rlbond


2009-06-11 18:25:58 8 Comments

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?

7 comments

@zangw 2015-12-25 07:13:09

Here is one good article of SFINAE: An introduction to C++'s SFINAE concept: compile-time introspection of a class member.

Summary it as following:

/*
 The compiler will try this overload since it's less generic than the variadic.
 T will be replace by int which gives us void f(const int& t, int::iterator* b = nullptr);
 int doesn't have an iterator sub-type, but the compiler doesn't throw a bunch of errors.
 It simply tries the next overload. 
*/
template <typename T> void f(const T& t, typename T::iterator* it = nullptr) { }

// The sink-hole.
void f(...) { }

f(1); // Calls void f(...) { }

template<bool B, class T = void> // Default template version.
struct enable_if {}; // This struct doesn't define "type" and the substitution will fail if you try to access it.

template<class T> // A specialisation used if the expression is true. 
struct enable_if<true, T> { typedef T type; }; // This struct do have a "type" and won't fail on access.

template <class T> typename enable_if<hasSerialize<T>::value, std::string>::type serialize(const T& obj)
{
    return obj.serialize();
}

template <class T> typename enable_if<!hasSerialize<T>::value, std::string>::type serialize(const T& obj)
{
    return to_string(obj);
}

declval is an utility that gives you a "fake reference" to an object of a type that couldn't be easily construct. declval is really handy for our SFINAE constructions.

struct Default {
    int foo() const {return 1;}
};

struct NonDefault {
    NonDefault(const NonDefault&) {}
    int foo() const {return 1;}
};

int main()
{
    decltype(Default().foo()) n1 = 1; // int n1
//  decltype(NonDefault().foo()) n2 = n1; // error: no default constructor
    decltype(std::declval<NonDefault>().foo()) n2 = n1; // int n2
    std::cout << "n2 = " << n2 << '\n';
}

@akim 2015-06-16 16:49:12

C++17 will probably provide a generic means to query for features. See N4502 for details, but as a self-contained example consider the following.

This part is the constant part, put it in a header.

// See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4502.pdf.
template <typename...>
using void_t = void;

// Primary template handles all types not supporting the operation.
template <typename, template <typename> class, typename = void_t<>>
struct detect : std::false_type {};

// Specialization recognizes/validates only types supporting the archetype.
template <typename T, template <typename> class Op>
struct detect<T, Op, void_t<Op<T>>> : std::true_type {};

The following example, taken from N4502, shows the usage:

// Archetypal expression for assignment operation.
template <typename T>
using assign_t = decltype(std::declval<T&>() = std::declval<T const &>())

// Trait corresponding to that archetype.
template <typename T>
using is_assignable = detect<T, assign_t>;

Compared to the other implementations, this one is fairly simple: a reduced set of tools (void_t and detect) suffices. Besides, it was reported (see N4502) that it is measurably more efficient (compile-time and compiler memory consumption) than previous approaches.

Here is a live example, which includes portability tweaks for GCC pre 5.1.

@odinthenerd 2014-08-11 11:46:29

In C++11 SFINAE tests have become much prettier. Here are a few examples of common uses:

Pick a function overload depending on traits

template<typename T>
std::enable_if_t<std::is_integral<T>::value> f(T t){
    //integral version
}
template<typename T>
std::enable_if_t<std::is_floating_point<T>::value> f(T t){
    //floating point version
}

Using a so called type sink idiom you can do pretty arbitrary tests on a type like checking if it has a member and if that member is of a certain type

//this goes in some header so you can use it everywhere
template<typename T>
struct TypeSink{
    using Type = void;
};
template<typename T>
using TypeSinkT = typename TypeSink<T>::Type;

//use case
template<typename T, typename=void>
struct HasBarOfTypeInt : std::false_type{};
template<typename T>
struct HasBarOfTypeInt<T, TypeSinkT<decltype(std::declval<T&>().*(&T::bar))>> :
    std::is_same<typename std::decay<decltype(std::declval<T&>().*(&T::bar))>::type,int>{};


struct S{
   int bar;
};
struct K{

};

template<typename T, typename = TypeSinkT<decltype(&T::bar)>>
void print(T){
    std::cout << "has bar" << std::endl;
}
void print(...){
    std::cout << "no bar" << std::endl;
}

int main(){
    print(S{});
    print(K{});
    std::cout << "bar is int: " << HasBarOfTypeInt<S>::value << std::endl;
}

Here is a live example: http://ideone.com/dHhyHE I also recently wrote a whole section on SFINAE and tag dispatch in my blog (shameless plug but relevant) http://metaporky.blogspot.de/2014/08/part-7-static-dispatch-function.html

Note as of C++14 there is a std::void_t which is essentially the same as my TypeSink here.

@T.C. 2014-09-21 05:43:36

Your first block of code redefines the same template.

@odinthenerd 2014-10-07 17:53:25

Since there is no type for which is_integral and is_floating_point are both true it should be an either or because SFINAE will remove at least one.

@T.C. 2014-10-07 17:56:27

You are redefining the same template with different default template arguments. Have you tried compiling it?

@odinthenerd 2014-10-07 18:14:26

Ah how stupid of me, thanks for pointing it out! Just fixed it.

@Kevin Doyon 2015-12-22 21:44:22

I'm new to template metaprogramming so I wanted to understand this example. Is there a reason you use TypeSinkT<decltype(std::declval<T&>().*(&T::bar))> at one place and then TypeSinkT<decltype(&T::bar)> at another? Also is the & necessary in std::declval<T&>?

@YSC 2018-12-12 14:46:23

About your TypeSink, C++17 have std::void_t :)

@whoan 2015-02-28 15:42:47

Here's another (late) SFINAE example, based on Greg Rogers's answer:

template<typename T>
class IsClassT {
    template<typename C> static bool test(int C::*) {return true;}
    template<typename C> static bool test(...) {return false;}
public:
    static bool value;
};

template<typename T>
bool IsClassT<T>::value=IsClassT<T>::test<T>(0);

In this way, you can check the value's value to see whether T is a class or not:

int main(void) {
    std::cout << IsClassT<std::string>::value << std::endl; // true
    std::cout << IsClassT<int>::value << std::endl;         // false
    return 0;
}

@Kirill Kobelev 2016-01-29 13:14:13

What does this syntax int C::* in your answer means? How can C::* be a parameter name?

@whoan 2016-01-29 15:00:12

It's a pointer to member. Some reference: isocpp.org/wiki/faq/pointers-to-members

@YSC 2018-12-12 14:49:31

@KirillKobelev int C::* is the type of a pointer to an int member variable of C.

@Greg Rogers 2009-06-11 18:54:41

Heres one example (from here):

template<typename T>
class IsClassT {
  private:
    typedef char One;
    typedef struct { char a[2]; } Two;
    template<typename C> static One test(int C::*);
    // Will be chosen if T is anything except a class.
    template<typename C> static Two test(...);
  public:
    enum { Yes = sizeof(IsClassT<T>::test<T>(0)) == 1 };
    enum { No = !Yes };
};

When IsClassT<int>::Yes is evaluated, 0 cannot be converted to int int::* because int is not a class, so it can't have a member pointer. If SFINAE didn't exist, then you would get a compiler error, something like '0 cannot be converted to member pointer for non-class type int'. Instead, it just uses the ... form which returns Two, and thus evaluates to false, int is not a class type.

@Johannes Schaub - litb 2009-06-12 23:25:43

@rlbond, i answered your question in the comments to this question here: stackoverflow.com/questions/822059/… . In short: If both test functions are candidates and viable, then "..." has the worst conversion cost, and hence will never be taken, in favor of the other function. "..." is the ellipsis, var-arg thing: int printf(char const*, ...);

@tstenner 2009-08-25 17:32:03

@HostileFork says dont trust SE 2012-07-12 06:01:40

The weirder thing here IMO is not the ..., but rather the int C::*, which I'd never seen and had to go look up. Found the answer for what that is and what it might be used for here: stackoverflow.com/questions/670734/…

@user2584960 2018-11-02 02:15:28

can someone explain what C::* is? I read all the comments and links, but I am still wondering, int C::* means that it is a member pointer of int type. what if a class has no member of int type? What am I missing? and how does test<T>(0) play into this? I must be missing something

@Johannes Schaub - litb 2009-06-12 23:40:28

I like using SFINAE to check boolean conditions.

template<int I> void div(char(*)[I % 2 == 0] = 0) {
    /* this is taken when I is even */
}

template<int I> void div(char(*)[I % 2 == 1] = 0) {
    /* this is taken when I is odd */
}

It can be quite useful. For example, i used it to check whether an initializer list collected using operator comma is no longer than a fixed size

template<int N>
struct Vector {
    template<int M> 
    Vector(MyInitList<M> const& i, char(*)[M <= N] = 0) { /* ... */ }
}

The list is only accepted when M is smaller than N, which means that the initializer list has not too many elements.

The syntax char(*)[C] means: Pointer to an array with element type char and size C. If C is false (0 here), then we get the invalid type char(*)[0], pointer to a zero sized array: SFINAE makes it so that the template will be ignored then.

Expressed with boost::enable_if, that looks like this

template<int N>
struct Vector {
    template<int M> 
    Vector(MyInitList<M> const& i, 
           typename enable_if_c<(M <= N)>::type* = 0) { /* ... */ }
}

In practice, i often find the ability to check conditions a useful ability.

@akim 2013-02-05 09:07:07

@Johannes Weirdly enough, GCC (4.8) and Clang (3.2) accept to declare arrays of size 0 (so the type is not really "invalid"), yet it behaves properly on your code. There is probably special support for this case in the case of SFINAE vs. "regular" uses of types.

@v.oddou 2014-06-13 10:43:21

@akim: if that is ever true (weird ?! since when ?) then maybe M <= N ? 1 : -1 could work instead.

@akim 2014-06-14 16:06:18

@v.oddou Just try int foo[0]. I'm not surprised it's supported, as it allows the very useful "struct ending with a 0-length array" trick (gcc.gnu.org/onlinedocs/gcc/Zero-Length.html).

@v.oddou 2014-06-16 01:31:41

@akim: yeah its what I thought -> C99. This is not allowed in C++, here is what you get with a modern compiler : error C2466: cannot allocate an array of constant size 0

@akim 2014-06-16 06:45:10

@v.oddou No, I really meant C++, and actually C++11: both clang++ and g++ accept it, and I have pointed to a page that explains why this is useful.

@David Joyner 2009-06-11 19:39:28

Boost's enable_if library offers a nice clean interface for using SFINAE. One of my favorite usage examples is in the Boost.Iterator library. SFINAE is used to enable iterator type conversions.

Related Questions

Sponsored Content

1 Answered Questions

[SOLVED] The Definitive C++ Book Guide and List

  • 2008-12-23 05:23:56
  • grepsedawk
  • 2276066 View
  • 4245 Score
  • 1 Answer
  • Tags:   c++ c++-faq

39 Answered Questions

22 Answered Questions

[SOLVED] What is the "-->" operator in C++?

17 Answered Questions

[SOLVED] How can I profile C++ code running on Linux?

  • 2008-12-17 20:29:24
  • Gabriel Isenberg
  • 500798 View
  • 1762 Score
  • 17 Answer
  • Tags:   c++ linux profiling

25 Answered Questions

[SOLVED] Why do we need virtual functions in C++?

13 Answered Questions

[SOLVED] What is the effect of extern "C" in C++?

12 Answered Questions

[SOLVED] Storing C++ template function definitions in a .CPP file

  • 2008-09-22 15:55:52
  • Rob
  • 319195 View
  • 498 Score
  • 12 Answer
  • Tags:   c++ templates

10 Answered Questions

24 Answered Questions

[SOLVED] Image Processing: Algorithm Improvement for 'Coca-Cola Can' Recognition

Sponsored Content