Template Metafunction Identity

last modified: May 29, 2006

The identity metafunction stores the type provided in value. The is_same metafunction uses partial template specialization to determine at compile time if two object types are the same.

namespace framework {
namespace meta
{
   template <typename T> struct identity { typedef T value; },;
   template <typename T0, typename T1> struct is_same { enum { value = false },; },;
   template <typename T> struct is_same<T, T> { enum { value = true },; },;

}, // namespace meta
}, // namespace framework

Examples:

identity<float>::value f = 3.14159f;
true == is_same<identity<int>::value, int>::value;

TemplateMetaprogrammingTechniques CategoryCppTemplates


Loading...