我试图为容器定义通用运算符,如下所示:

#include <algorithm>
#include <functional>

namespace test {

template<template <typename...> class _Container,
         typename _Type, typename... _Args>
_Container<_Type,_Args...>
operator+(const _Container<_Type,_Args...>& c1,
          const _Container<_Type,_Args...>& c2)
{
  typedef _Container<_Type,_Args...> container_type;

  assert(c1.size() == c2.size());

  container_type result;

  std::transform(c1.begin(), c1.end(), c2.begin(),
                 std::back_inserter(result), std::plus<_Type>());

  return result;
}

} // test namespace

但是,GCC 4.9.2不会尝试使用以下测试代码:
typedef std::vector<int> vector;
vector v1, v2;
vector result = v1 + v2;

我也尝试了上面没有模板参数包的情况。结果相同。

但是,如果没有 namespace 声明,它就可以正常工作。

我究竟做错了什么?由STL在std命名空间中定义的类似运算符将作为候选者进行测试。

错误消息很简单:
/tmp/file.cc: In function ‘int main()’:
/tmp/file.cc:28:22: error: no match for ‘operator+’ (operand types are ‘vector {aka std::vector<int>}’ and ‘vector {aka std::vector<int>}’)

最佳答案



呃哦

忽略您将遇到的ADL问题,也存在语义问题。

例如,考虑:

vector<int> a { 1, 2, 3 };
vector<int> b { 4, 5, 6 };

auto c = a + b;   // this won't compile, it's for illustration.

问题:手术应该怎么做?

某些人可能认为它应该对此建模:
auto c = concatenate(a, b);
// c == { 1, 2, 3, 4, 5, 6 }

其他人可能会认为它应该按照您的建议进行:
auto c = add_elements(a, b);
// c == { 5, 7, 9 }

谁是对的?

答案是,这取决于使用 vector 的上下文。 vector 是原始类型。它不包含有关用例的信息。根本没有足够的信息可用于做出明智的选择。

将 vector 包装为自定义类型可让您提供上下文信息并正确描述运算符(operator)的 Action 。

当然,您将需要为类型明确定义算术运算符。

综上所述:

标准库没有充分理由为容器定义算术运算符。出于同样的原因,您也不应该。

值得一提的是,即使变换解释也不是简单的。如果 vector 大小不同怎么办?

关于c++ - 未解析为候选容器的模板运算符模板,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37226687/

10-09 06:34
查看更多