2

I'm trying to write some stuff to wrap simd intrinsics, and I figured that it would be nice to do something like

using vec<float, 4> = __m128;

so that I could use templates in some code I write later on. This isn't really relevant to the question, but __m128 is a type that represents 4 floats. This doesn't work, and g++ says expected nested-name-specifier before 'vec'. I know I can just write classes to wrap them, or I can do something like:

template <typename T, int N> struct vec;

template<> struct vec<float, 4>
{
    typedef __m128 type;
};

and then I can use vec<float,4>::type, but the first way is far more convenient. I think it might be possible using something like C++ template typedef, but I'm not sure, and I wouldn't know the syntax. Is there any way to make the first statement work or to do something similar?

BadProgrammer99
  • 759
  • 1
  • 5
  • 13

1 Answers1

4

No, the first can't work. It doesn't make sense given the current rules (you can't hijack the syntax of a template like that).

Your second solution however is perfect! You just need to make a slight adjustment:

namespace impl {
    template <typename T, int N> struct vec;

    template<> struct vec<float, 4>
    {
        typedef __m128 type;
    };
}

template<typename T, int N>
using vec = typename impl::vec<T, N>::type;

Now vec is an actual template referring to the type of the corresponding struct.

Rakete1111
  • 47,013
  • 16
  • 123
  • 162