简体   繁体   English

模板类的模板化等式运算符无法编译

[英]Templated Equality Operator for Template Class Does Not Compile

I've written the following code intended to allow my class, mytype , to select at compile-time whether to use a C-style array or a C++ STL array, like so: 我编写了以下代码,目的是允许我的类mytype在编译时选择使用C样式数组还是C ++ STL数组,如下所示:

#include<array>
#include<cassert>

template<bool IsTrue, typename IfTrue, typename IfFalse>
struct choose;

template<typename IfTrue, typename IfFalse>
struct choose<true, IfTrue, IfFalse> {
    typedef IfTrue type;
};

template<typename IfTrue, typename IfFalse>
struct choose<false, IfTrue, IfFalse> {
    typedef IfFalse type;
};

template<bool ArrayIsRaw>
struct mytype {
    typedef typename choose<ArrayIsRaw, int[50], std::array<int, 50>>::type array_t;
    array_t data{};
};

int main() {
    mytype<true> raw_version;
    mytype<false> stl_version;
    raw_version.data[5] = 15;
    stl_version.data[15] = 5;
    raw_version.data[10] = stl_version.data[15];
    assert(raw_version.data[10] == 5);
    return 0;
}

This works just fine. 这样很好。 However, I want to add an equality operator to this class that is agnostic of the underlying types involved: basically, I want raw_version == stl_version to be valid, compileable code which will return true if each element is identical. 但是,我想向此类添加一个与所涉及的基础类型无关的相等运算符:基本上,我希望raw_version == stl_version是有效的可编译代码,如果每个元素相同,则返回true

But when I add the following code to my class definition: 但是,当我在类定义中添加以下代码时:

template<bool Raw1, bool Raw2>
friend bool operator==(mytype<Raw1> const& a, mytype<Raw2> const& b) {
    for(size_t i = 0; i < 50; i++) if(a.data[i] != b.data[i]) return false;
    return true;
}

I get the following error: 我收到以下错误:

prog.cpp: In instantiation of ‘struct mytype<false>’:
prog.cpp:32:16:   required from here
prog.cpp:24:14: error: redefinition of ‘template<bool Raw1, bool Raw2> bool operator==(const mytype<Raw1>&, const mytype<Raw2>&)’
  friend bool operator==(mytype<Raw1> const& a, mytype<Raw2> const& b) {
              ^~~~~~~~
prog.cpp:24:14: note: ‘template<bool Raw1, bool Raw2> bool operator==(const mytype<Raw1>&, const mytype<Raw2>&)’ previously defined here

What do I need to do to fix this error? 我需要怎么做才能解决此错误?

By making both arguments to operator== templated, you are redefining this exact function template for mytype<true> and mytype<false> . 通过将operator==两个参数都模板化,您将为mytype<true>mytype<false>重新定义此确切的函数模板。 Simply remove the templating from the first (or second, but not both) argument to make this work: 只需从第一个(或第二个,但不是两个)参数中删除模板即可完成此工作:

template<bool Raw2>    
friend bool operator==(mytype const& a, mytype<Raw2> const& b) {    
    // ...   
}

It also appears that your choose is just an implementation of std::conditional and you could instead have 看来您choose的只是std::conditional的实现,而您可以选择

using array_t = typename std::conditional<ArrayIsRaw, int[50], std::array<int, 50>>::type;

or for c++14 或对于c ++ 14

using array_t = std::conditional_t<ArrayIsRaw, int[50], std::array<int, 50>>;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM