我试图为容器定义一个通用运算符,如下所示:
#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;
我也尝试了上面没有模板参数包的方法。相同的结果。
但是,如果没有命名空间声明,它也能正常工作。
我做错了什么?在 std
命名空间中由 STL 定义的类似运算符作为候选进行测试。
错误信息很简单:
/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>}’)
最佳答案
I was trying to define a generic operator for containers
呃哦...
忽略您将遇到的 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 包装成自定义类型允许您提供上下文信息并正确描述运算符的操作。
当然,您需要为类型显式定义算术运算符。
总结:
标准库没有为容器定义算术运算符是有充分理由的。出于同样的原因,你也不应该。
作为临别注,即使是转换解释也不是微不足道的。如果 vector 大小不同会怎样?
关于c++ - 未解析为候选容器的模板运算符模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37226687/