CustomVariant; // som-6ren">
gpt4 book ai didi

c++ - 将 std::vector> 与 Google Mock 一起使用会导致编译错误

转载 作者:行者123 更新时间:2023-11-27 23:51:09 25 4
gpt4 key购买 nike

有这段代码:

#include "gmock/gmock.h"
#include <boost/variant.hpp>

typedef boost::variant<std::vector<unsigned char>, std::vector<int>> CustomVariant;

// some overloads which don't work
std::ostream& operator<<(
std::ostream& stream,
const boost::variant<std::vector<unsigned char>>&)
{ return stream; }

std::ostream& operator<<(
std::ostream& stream,
const boost::variant<std::vector<int>>&)
{ return stream; }

std::ostream& operator<<(
std::ostream& stream,
const std::vector<unsigned char>&)
{ return stream; }

std::ostream& operator<<(
std::ostream& stream,
const std::vector<int>&)
{ return stream; }

class MyClass
{
MOCK_METHOD1(fun, bool(std::vector<CustomVariant> v));
};

int main()
{
MyClass a;
return 0;
}

有两个错误:

/usr/include/boost/variant/detail/variant_io.hpp:64:14: error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream<char>’ and ‘const std::vector<unsigned char>’)
out_ << operand;
/usr/include/boost/variant/detail/variant_io.hpp:64:14: error: cannot bind ‘std::basic_ostream<char>’ lvalue to ‘std::basic_ostream<char>&&’
out_ << operand;

它提示没有定义 operator<<对于类型 std::basic_ostream<char>const std::vector<unsigned char> ,尽管它似乎已被定义。我尝试了一些重载,但没有一个有效。如何正确编译这段代码?

在 g++ 6.3 上编译:

g++ main.cpp -lgmock -o main -L ./googletest-release-1.8.0/googlemock -pthread

最佳答案

boost::operator<<(std::ostream&, boost::variant const&)boost/variant/detail/io.hpp 中定义不幸的是,遵循 ADL 发现的 operator<< .

如前所述,没有 operator<<(std::ostream&, std::vector<> const&)在 std 命名空间中声明,声明一个是非法的。

解决方法是将此运算符注入(inject) boost::detail::variant命名空间。

您不会希望在生产代码中执行此操作,因为它依赖于对 boost 内部知识的了解,但在测试中它可能是可以接受的。

这个编译:

#include "gmock/gmock.h"
#include <vector>

struct emitter
{
emitter(std::ostream& os) : os(os) {};

template<class T> std::ostream& operator()(T const& v) const
{
return os << v;
}

std::ostream& operator()(char c)
{
if (std::isprint(c))
{
return os << '\'' + c + '\'';
}
else
{
auto oldstate = os.flags();
os << std::hex << "0x" << (int(c) & 0xff);
os.flags(oldstate);
return os;
}
}


template<class T, class A>
std::ostream &operator()(const std::vector<T, A> &v) const
{
const char* sep = " ";
os << "[";
for (auto&& x : v)
{
(*this)(x);
sep = ", ";
}
return os << " ]";
}


std::ostream& os;
};

namespace boost { namespace detail { namespace variant {
template<class T, class A>
std::ostream &operator<<(std::ostream &os, std::vector<T, A>const &var)
{
auto e = emitter(os);
return e(var);
}

}}}

#include <boost/variant.hpp>
#include <iomanip>




typedef boost::variant<std::vector<unsigned char>, std::vector<int>> CustomVariant;





class MyClass
{
MOCK_METHOD1(fun, bool(std::vector<CustomVariant>v));
};

int main()
{
MyClass a;
return 0;
}

关于c++ - 将 std::vector<boost::variant<...>> 与 Google Mock 一起使用会导致编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46343138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com