My blog
My LinkedIn Profile

BOOKS i'm reading

Cryptography engineering, Niels Ferguson, Bruce Schneier, Tadayoshi Kohno, ISBN: 9780470474242
Advanced Programming in the UNIX(R) Environment (2nd Edition), W. Richard Stevens, Stephen A. Rago, ISBN:0201433079
Trading For a Living, Alexander Elder, ISBN:0471592242


What is the C++ SFINAE principle ?


Permalink 01:17:33 pm, by lano1106, 322 words, 6001 views   English (CA)
Categories: C++

What is the C++ SFINAE principle ?

This is an acronym that means "substitution-failure-is-not-an-error" and it is used in the context of template functions overloading. When the compiler evaluates each overloaded template functions, it will not emit an error if by using the template parameter on one of the potential candidate function would generate an error. The compiler will just discard that function from the list of potential candidates. It is a powerful construction that allows metaprogramming as you can evaluate expressions at compile-time. Here is an example from the book C++ templates:

template<typename T>
class IsClassT {
    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(...);
    enum { Yes = sizeof(IsClassT<T>::template test<T>(0)) == 1 };
    enum { No = !Yes };

If T is a class, IsClassT<T>::Yes will be true because the first test() function will be chosen by the compiler. From the C++ templates book:

overload resolution prefers the conversion from zero to a null pointer constant over binding an argument to an ellipsis parameter (ellipsis parameters are the weakest kind of binding from an overload resolution perspective).

There is one subtle detail that is important enough to mention it. It is the usage of the template keyword at the Yes enumeration declaration. The reason behind the need for using the keyword is similar to why you sometime need to use the keyword typename.

It is because of template specialization, the compiler has no clue what the dependent name "test" is. Without explicit indication with the keyword template, it is interpreting "<" and ">" as "greater than" and "lower than" logical operators.

Maybe you were already using the principle without knowing its name but now next time you see SFINAE in a C++ forum, you will know what they are talking about.

Comments, Pingbacks:

Comment from: Michal [Visitor] Email
Hallo Olivier.
Thank You for Your example.

So far I am trying to understand how it works.

Unfortunatelly I have problem with preliminary step: it does not compile on my linux box (g++ (GCC) 4.3.0 20080428 (Red Hat 4.3.0-8)).

I get the following error:
p41.cpp:12: error: expected primary-expression before ">" token

Line 12 in my program is the following:

enum { Yes = sizeof(IsClassT...::test....(0)) == 1 };

(sorry for modifications in line above, but I couldn't put template parameter T in Your page, because they have a format of tag).

have You got idea why doesn't it compile?

best regards
PermalinkPermalink 08/31/09 @ 01:29
Comment from: lano1106 [Member]

To be honest, I had an intellectual understanding of the example I was giving but I have
never bothered to compile it to see if it works until today.

I have tried to compile it with gcc 3.4.4 and I get the same error message. I will try to
find an answer and post it if possible.

Thank you for the comment.
PermalinkPermalink 08/31/09 @ 20:22

This post has 10 feedbacks awaiting moderation...

Comments are closed for this post.

Olivier Langlois's blog

I want you to find in this blog informations about C++ programming that I had a hard time to find in the first place on the web.

July 2015
Sun Mon Tue Wed Thu Fri Sat
 << <   > >>
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  


Custom Search


XML Feeds

What is RSS?

Who's Online?

  • Guest Users: 8

powered by