【问题标题】:Constructor templated by integer constant [duplicate]由整数常量模板化的构造函数[重复]
【发布时间】:2017-09-07 16:46:03
【问题描述】:

因为_mm_extract_epi64() 将一个必须在编译时知道的常量作为参数,所以我正在尝试实现一个以整数常量为模板的构造函数:

  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) { }
  };

但是,当我尝试使用构造函数时,使用这种语法

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

我在上面的最后一行遇到编译器错误:

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

你能帮忙看看正确的语法吗?

【问题讨论】:

  • @Rakete1111,我的问题是关于非类型参数的。我也看过这个问题:stackoverflow.com/questions/3960849/c-template-constructor。他们在那里提出了模板类型参数的解决方案。所以我想弄清楚是否可以为非类型参数实现类似的东西,但到目前为止还不能达到正确的语法......
  • @Rakete1111,现在我看到了 Gruffalo 的解决方法。但是如果作为参数传递,C++ 不能推断出非类型模板参数吗?我的意思是对我发布的代码没有一些修复,而不是将其完全改造成不同的解决方法?
  • 我实际上没有,也许它毕竟不是重复的 :) 谢谢
  • 这个stackoverflow.com/questions/8837113/… 表明无法修复我的代码,所以也许这个问题确实是重复的。
  • 它是重复的,因为接受的答案表明没有办法“修复”您的代码,只有一种解决方法

标签: c++ templates c++17 constexpr non-type


【解决方案1】:

https://stackoverflow.com/a/16944262/453271一样,只是模板参数是非类型的:

template<int I>
struct id
{};

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

A a{id<0>{}};

【讨论】:

    猜你喜欢
    • 2011-05-24
    • 1970-01-01
    • 1970-01-01
    • 2013-05-30
    • 2020-06-13
    • 1970-01-01
    • 1970-01-01
    • 2013-04-21
    • 1970-01-01
    相关资源
    最近更新 更多