0

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?

Serge Rogatch
  • 13,865
  • 7
  • 86
  • 158
  • @Rakete1111, my question is specifically about non-type parameters. I had also seen this question: https://stackoverflow.com/questions/3960849/c-template-constructor . There they suggest solutions for template type parameters. So I'm trying to figure out if something similar can be implemented for non-type parameters, but can't hit the correct syntax so far... – Serge Rogatch Sep 07 '17 at 16:52
  • @Rakete1111, now I see Gruffalo's workaround. But can't C++ deduce non-type template argument if it's passed as a parameter? I mean isn't there some fix for the code I posted, rather than reworking it completely to a different workaround? – Serge Rogatch Sep 07 '17 at 17:17
  • I don't actually, maybe it's not a duplicate after all :) Thanks – Rakete1111 Sep 07 '17 at 17:19
  • This https://stackoverflow.com/questions/8837113/deduce-non-type-template-parameter suggests that there is no way to fix my code, so perhaps the question is indeed a duplicate. – Serge Rogatch Sep 07 '17 at 19:09
  • it is duplicate because the accepted answer states that there's no way to "fix" your code, only a workaround – Andriy Tylychko Sep 07 '17 at 22:50

1 Answers1

0

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>{}};
Andriy Tylychko
  • 15,967
  • 6
  • 64
  • 112