C++ 在Google Test中使用自定义插入运算符会引发“二进制表达式的操作数无效”编译错误
使用clang-3.6编译此代码C++ 在Google Test中使用自定义插入运算符会引发“二进制表达式的操作数无效”编译错误,c++,operator-overloading,clang,googletest,argument-dependent-lookup,C++,Operator Overloading,Clang,Googletest,Argument Dependent Lookup,使用clang-3.6编译此代码 但是这次ostream没有使用std进行限定,也许这允许名称查找成功?这是预期的行为。你的操作符great,所以如果我将声明移到include之上,这是可行的,但是我在实际代码中很难做到这一点。我包括我的标题,它定义了Operator是否为您工作?看起来不错,如果您将其作为答案提交,我将接受它。我希望能有办法保留原来的语法,但似乎不可能。这里也有同样的问题。仅在Clang上找到,但在GCC或MSVC上未找到。在我的代码中,我可以将“gtest”的include移
但是这次ostream没有使用std进行限定,也许这允许名称查找成功?这是预期的行为。你的操作符great,所以如果我将声明移到include之上,这是可行的,但是我在实际代码中很难做到这一点。我包括我的标题,它定义了Operator是否为您工作?看起来不错,如果您将其作为答案提交,我将接受它。我希望能有办法保留原来的语法,但似乎不可能。这里也有同样的问题。仅在Clang上找到,但在GCC或MSVC上未找到。在我的代码中,我可以将“gtest”的include移到内部头的include之后。当然,这不是一个干净的解决方案。
#include "gtest/gtest.h"
#include <vector>
#include <iostream>
template<typename T>
std::ostream& operator<< (std::ostream& os, const std::vector<T>& v)
{
for (const auto &it : v)
os << it << " ";
return os;
}
TEST(Case, Test)
{
bool foo{false};
std::vector<int> v{1,2,3};
EXPECT_TRUE(foo) << v;
}
gtest/include/gtest/gtest-message.h:131:10: error: invalid operands to binary expression
('std::basic_stringstream<char>' and 'const std::vector<int, std::allocator<int> >')
*ss_ << val;
~~~~ ^ ~~~
test.cpp:23:13: error: invalid operands to binary expression
('ostream' (aka 'basic_ostream<char>') and 'std::vector<int>')
std::cout << v;
~~~~~~~~~ ^ ~