Constructor templated by integer constant

250 Views Asked by At

Because _mm_extract_epi64() takes as a parameter a constant that must be known at compile time, I'm trying to implement a constructor templated by integer constant:

  union Packed64 {
    double _f64;
    float _f32[2];
    uint64_t _u64;
    int64_t _i64;
    uint32_t _u32[2];
    int32_t _i32[2];
    uint16_t _u16[4];
    int16_t _i16[4];
    uint8_t _u8[8];
    int8_t _i8[8];

    Packed64() { }

    template<uint8_t taAt> explicit Packed64(const __m128i& vect, const uint8_t taAt)
      : _i64(_mm_extract_epi64(vect, taAt)) { }
    explicit Packed64(const uint64_t valU64) : _u64(valU64) { }
  };

However, with this syntax when I try to use the constructor like

const __m128i a = /* calculated here */;
Packed64 p64(a, 0);

I'm getting a compiler error on the last line above:

error C2661: 'Packed64::Packed64': no overloaded function takes 2 arguments

Could you help with the correct syntax?

1

There are 1 best solutions below

0
On

It's same as in https://stackoverflow.com/a/16944262/453271 just with non-type template parameter:

template<int I>
struct id
{};

struct A {
  template<int I>
  A(id<I>)
  {}
};

A a{id<0>{}};