Into

Modules

Documentation

classPii::OnlyPrimitive

#include <PiiTypeTraits.h>

A structure that has a nested type Type if and only if T is a primitive type.

Description

This can be exploited as a substitution failure is not an error (SFINAE) trick to guide overload resolution.

 // I want this function to be called for primitive types only
 template <class T> void func(T value, typename Pii::OnlyPrimitive<T>::Type = 0)
 {}

 // This will be called for Base and all derived types.
 void func(const Base& base)
 {}

 // Derived inherits Base
 Derived d;
 func(d); // calls func(const Base&)

If you left the second dummy parameter out, the template function would be called for types derived from Base, as the template type T is always an exact match. Since Pii::OnlyPrimitive<T>::Type only exists if T is a primitive type, the first function causes a substitution failure for all other types, and will be left out of oveload resolution.

Notes (0)

Add a note

Not a single note added yet. Be the first, add yours.