Friday, April 15, 2011

Cómo especializar un template para arrays (II)

Siempre se aplica el más restrictivo, o lo que es lo mismo, el más especializado. Ejemplo:

#include <iostream>

template < typename T >
struct Example
{
    static inline void call()
    {
        std::cout << "non-array" << std::endl;
    }
};

template < typename T, unsigned int N >
struct Example< T[N] >
{
    static inline void call()
    {
        std::cout << "array" << std::endl;
    }
};


template < unsigned int N >
struct Example< int[N] >
{
    static inline void call()
    {
        std::cout << "integer array" << std::endl;
    }
};

int main()
{
    Example < int[5][10] >::call();
    Example < int[10] >::call();
    Example < float[10] >::call();
    Example < float >::call();
}

Resultado:

array
integer array
array
non-array

No comments:

Post a Comment