gpt4 book ai didi

c++ - ADL 无法找到具有用户定义类型的适当限定符的流运算符

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:32:21 25 4
gpt4 key购买 nike

我正在使用 Visual Studio 2010 在 Microsoft Windows 7 上编译 x64 服务,使用 Boost variant像这样的东西:

namespace my_ns
{
typedef struct {} empty_t;
typedef std::pair<size_t, std::shared_ptr<char>> string_t;
typedef boost::variant<empty_t, double, long, string_t> variant_t;
typedef std::map<unsigned short, variant_t> variant_map_t;
}

我摆脱那个 string_t 并用 std::string 替换它的那一天就是我给我的老板和团队买 donut 的那一天。但这不是我们在这里的原因......

Boost 变体支持其包含类型的流运算符,前提是该类型具有重载。所以我有:

namespace my_ns
{
std::ostream &operator<<(std::ostream &, const empty_t &);
std::ostream &operator<<(std::ostream &, const string_t &);
}

然而,我却被错误消息所困扰:

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const T3' (or there is no acceptable conversion)

T3 指的是 string_t。

生成错误的违规代码存在于以下上下文中。它很冗长,所以你,读者,有相关的上下文信息:

namespace my_ns
{
void Widget::public_method(std::ostringstream &os) const
{
//variant_map_t Widget::the_map; // Private Widget member.

// This here is a C++11 lambda in a standard loop algorithm, if you didn't recognize the syntax.
std::for_each(the_map.begin(), the_map.end() [&os](variant_map_t::value_type value)
{
os << value.first << '=' << value.second << ' ';
});
}
}

我曾尝试删除右侧的限定符和引用,认为按值传递拷贝会取消限定符(根据共享指针可能不是那么出色),并且我尝试将声明从命名空间移动到全局范围,希望 ADL 会出于某种原因接受它(从概念上讲,我得到了 ADL,但对我来说仍然只有一点黑魔法)。

我不知道还能做什么。除了编译器无法定位具有 const 限定 rhs 的插入运算符之外,这个错误的本质是什么?当它就在那里时,怎么可能呢?分辨率是多少?

最佳答案

将 typedef 添加到命名空间没有帮助,std::pairstd::ostream仍然是 std 的一部分命名空间。所以编译器会在那里寻找 <<运算符,而不是在您的命名空间中。

关于c++ - ADL 无法找到具有用户定义类型的适当限定符的流运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10590898/

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