C/C++ Madrid meetup @ biicode office

Today is the day! We host the C/C++ Madrid meetup

It’s finally here and full of interesting content. The C and C++ community gathers together to talk about metaprogramming.

C/C++ Madrid meetup hosted at biicode

On the shoulders of giants

Manu Sánchez will be talking about template metaprogramming. He already addressed this issue in a couple of posts in our blog (read them HERE and HERE).

Final templates tips: Duck typing and SFINAE

C++ templates are processed in two phases: Declaration and instantiation. First the compiler checks the declaration of the template for syntactical correcness, but what happens to things that depends on template parameters?
The compiler doesn’t know anything about that parameters until template instantiation, so it delays the analysis of such things until the second phase, which is executed after a template is instanced.

Consider this function template:

T could be any type, so the question is: Has this type a member function f()?
The answer is: Nobody knows, and the compiler doesn’t care. Until template instantiation.
So if you use that template correctly, using a type T with a member function f(), the code compiles and works. But, if the type is not correct? Is that a compilation error? Enter SFINAE.

SFINAE

SFINAE, Specialization Failure Is Not An Error, is the colloquial name of a rule on the C++ Language Standard saying that a failure when trying to instantiate a template is not a compilation error, but the compiler continues trying with other alternatives (More templates or overloads).

Following with the duck typing example, if we use the function template with a type with no member function f(), that doesn’t result in a compilation error (And then the compiler aborting compilation), but the compiler continues searching for other template or overload that matches the call.

That behavior could be used to “enable” a template only if the template parameters passed fulfill a certain property or requirement. For example:

That template could be used with floating-point parameters only. Note the std::enable_if template there. That template provided by the standard library allows us to use SFINAE in an easy way. What it does is to declare a member type ::type if a certain boolean condition is true.
If we reference that member (typename std::enable_if::type and the condition is false, than the template has no type member, and the template is ill-formed. But do you remember the SFINAE rule? Instead of aborting compilation because the ill-formed template, the compiler continues.

So the result is that we “disabled” that template because the parameter was not a floating-point type. Cool, isn’t?

It’s never too late to sing up for the C/C++ Madrid meetup!!

 

Madrid C/C++

Madrid, ES
178 programadores

Conoce otros programadores C/C++ de Madrid. Haz Networking, aprende, o simplemente disfruta de unas cervezas. Todos los niveles de conocimiento son bienvenidos, incluso princi…

Next Meetup

De 0 a 100 (Taller): metaprogramación con plantillas en C++ …

Thursday, Oct 2, 2014, 7:00 PM
39 Attending

Check out this Meetup Group →

 

We will try to broadcast the event through hangouts, check below:

C/C++ Madrid meetup Hangouts channel


Related Posts
  • Mike

    “Has this type a member function f()?” Typo here, the name of a member function should be g()