Type Specialization

template<typename T>
class vec3
{
public:
    typename T type_t;
    T x;
    T y;
    T z;
};

template<typename T>
struct numeric_type_traits_basic_c
{
    typedef T type_t;
    typedef T scalar_t;
};

template<typename T>
struct numeric_type_traits_vec3_c
{
    typedef T type_t;
    typedef typename T::type_t scalar_t;
};

typedef numeric_type_traits_basic_c<int> int_type_traits;
typedef numeric_type_traits_vec3_c< vec3<int> > vec3_int_type_traits;

These are characteristic features for scalar and vector, the only difference is that the scalar type for a vector is the type of its element. It works great.

But I would really like to have the same name for these two classes.

template<typename T>
struct numeric_type_traits_c
{
    typedef T type_t;
    typedef ????? scalar_t;
};

I know that this is possible if the class is explicitly specialized for each type that I need: int, float, vec3, vec3 ...

This is a lot of duplication ... How can I keep the simplicity of the first bit of code, but at the same time have the same class name?

+5
source share
4 answers

This is the syntax for specialized partial classification of templates:

template<typename T>
struct numeric_type_traits // basic template
{
    typedef T type_t;
    typedef T scalar_t;
};

template<typename T>
struct numeric_type_traits< vec3<T> > // partial specialisation for vec3's
{
    typedef vec3<T> type_t;
    typedef T scalar_t;
};

And so on, for example:

template <typename T, typename T_Alloc>
struct numeric_type_traits< std::vector<T,T_Alloc> > // part. spec. for std::vector
{
    typedef std::vector<T,T_Alloc> type_t; // deal with custom allocators, too
    typedef T scalar_t;
};
+5
source

, ? :

template<typename TYPE, typename SCALAR>
struct numeric_type_traits_c
{
   typedef TYPE type_t;
   typedef SCALAR scalar_t;
};

typedef numeric_type_traits_c<int,int> int_type_traits;
typedef numeric_type_traits_c<vec3<int>, vec3<int>::type_t> vec3_type_traits;
0
template<typename T>
struct numeric_type_traits_c
{
        typedef T type_t;
        typedef T scalar_t;
};

template<typename T>
struct numeric_type_traits_c<vec3<T> >
{
        typedef vec3<T> type_t;
        typedef typename vec3<T>::type_t scalar_t;
};

, , type_t vec3!

0

, , " ".

f:

struct I { virtual double f()const = 0; }; // C++ version of 'an interface'
struct A : public I { virtual double f()const{ return 0; } };
struct B : public I { virtual double f()const{ return 1; } };
struct C { };

void f( const I& i ){ return I.f(); }

f( A() );
f( C() ); // compiler warning: wrong type provided.

f:

// struct TI { typedef ??? iT; }; // no C++ version of a type-interface
struct TA { typedef int iT; };
struct TB { typedef double iT; };
struct TC { };

template< typename aTI > struct fT { typedef aTI::iT returnType; };

fT< TA >::returnType vA; 
ft< C  >::returnType vC; // compiler error: C has no iT member.

, - . "" . , .

fT is a function that can only be used by the compiler to determine a specific type. It takes a type as an argument. But the language does not have a "concept" of type restriction (for more, see "Concepts in C ++ 0x"). Therefore, we need to manually ensure that the types that we use for the type function "implement" the correct interface.

In concreto, it comes down to adding a type scalar_tto any class that you want to use with a type function numeric_type_traits_c.

0
source

All Articles