Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 模板参数数组_C++_C++11_Templates_Galois Field - Fatal编程技术网

C++ 模板参数数组

C++ 模板参数数组,c++,c++11,templates,galois-field,C++,C++11,Templates,Galois Field,我正在尝试创建一个类,该类使用数组的值作为值/类型模板参数。见下文 template< std::array<bool, M> arr> class gf { ... }; 我当前的解决方法是将arr传递给构造函数调用,并在组合任何不兼容类型时抛出错误。我希望有更好的方法,谢谢 编辑: 此外,任何其他可能实现相同目标的设计模式都将受到欢迎。我不习惯使用模板专业化,但这是一个我熟悉的工具 纯粹从技术角度来看,您可以(1)将具有静态存储持续时间的constepr数组的地址作

我正在尝试创建一个类,该类使用数组的值作为值/类型模板参数。见下文

template< std::array<bool, M> arr>
class gf {
...
};
我当前的解决方法是将
arr
传递给构造函数调用,并在组合任何不兼容类型时抛出错误。我希望有更好的方法,谢谢

编辑:
此外,任何其他可能实现相同目标的设计模式都将受到欢迎。我不习惯使用模板专业化,但这是一个我熟悉的工具

纯粹从技术角度来看,您可以(1)将具有静态存储持续时间的
constepr
数组的地址作为非类型模板参数传递:

#include <array>

template<const auto& arr>
struct gf {
    gf& operator+=(const gf& /* rhs */) {
        // ...
        return *this;
    }
};

template<const auto& arr>
auto operator+(gf<arr> lhs, const gf<arr>& rhs) {
    lhs += rhs;
    return lhs;
}

int main() {
    // As we want to use the address of the constexpr std::array
    // at compile time, it needs to have static storage duration.
    static constexpr std::array<bool, 3U> p1{{0, 0, 1}};
    static constexpr std::array<bool, 3U> p2{{0, 1, 1}};
    
    gf<p1> a;
    gf<p1> b;
    gf<p2> c;
    
    auto out1 = a + b;  // OK.
    //auto out2 = a + c;  // Error: incompatible types.
}


(1) 这个答案在任何方面都不会试图将其作为解决OP中XY类问题的一种好方法。

arr
是一个
std::array
那么如何组合任何不兼容的类型呢?你能展示一下模板的一些示例用法和你想要的结果吗?看看我的编辑,让我知道更多信息是否有用。那么如果
a+c
有效,你期望的结果是什么?理想情况下
a+c
不会编译,因为我会添加不兼容的类型。如果它确实通过了编译,它将抛出一个运行时错误;gfd;a+d也可以编译吗?谢谢你的详细回答!我可以理解为什么这可能不是一个推荐的方法。在您看来,对于这种类型的问题,什么样的设计模式更好?因为我没有看到任何其他人对我的设计模式给出额外的反馈,我将接受这个答案,因为它解决了我对can/如何实现行为的关注。我仍然很想听听对如何实现我的目标有更好想法的人的意见。@ytgsyk4h我错过了你之前的评论:除了在C++20中使用文字类作为非类型模板参数之外,我想不出什么好主意。
#include <array>

template<const auto& arr>
struct gf {
    gf& operator+=(const gf& /* rhs */) {
        // ...
        return *this;
    }
};

template<const auto& arr>
auto operator+(gf<arr> lhs, const gf<arr>& rhs) {
    lhs += rhs;
    return lhs;
}

int main() {
    // As we want to use the address of the constexpr std::array
    // at compile time, it needs to have static storage duration.
    static constexpr std::array<bool, 3U> p1{{0, 0, 1}};
    static constexpr std::array<bool, 3U> p2{{0, 1, 1}};
    
    gf<p1> a;
    gf<p1> b;
    gf<p2> c;
    
    auto out1 = a + b;  // OK.
    //auto out2 = a + c;  // Error: incompatible types.
}
#include <array>

template<std::size_t M, const std::array<bool, M>& arr>
struct gf {
    gf& operator+=(const gf& /* rhs */) {
        // ...
        return *this;
    }
};

template<std::size_t M, const std::array<bool, M>& arr>
gf<M, arr> operator+(gf<M, arr> lhs, const gf<M, arr>& rhs) {
    lhs += rhs;
    return lhs;
}

int main() {
    // As we want to use the address of the constexpr std::array
    // at compile time, it needs to have static storage duration.
    static constexpr std::array<bool, 3U> p1{{0, 0, 1}};
    static constexpr std::array<bool, 3U> p2{{0, 1, 1}};
    
    gf<3U, p1> a;
    gf<3U, p1> b;
    gf<3U, p2> c;
    
    auto out1 = a + b;  // OK.
    //auto out2 = a + c;  // Error: incompatible types.
}