ADL 无法找到具有用户定义类型的适当限定符的流运算符
ADL can't locate stream operator with appropriate qualifiers for a user defined type
我正在使用Visual Studio 2010在Windows 7 Microsoft编译x64服务,使用Boost变体如下:
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 取而代之的那一天,就是我买老板和团队甜甜圈的那一天。但这不是我们在这里的原因...
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::pair
和std::ostream
仍然是 std
命名空间的一部分。因此,编译器将在那里查找<<
运算符,而不是在您的命名空间中查找。
相关文章:
- 有没有一种方法可以通过"typedef"为重新定义的基本类型定义特征和强制转换运算符
- JS相等运算符(如===)是否可以使用embind类型
- 为什么在我的函数类型后使用引用运算符 (&) 允许我修改它返回的值?
- 将所有模板类型传递给运算符,而不指定所有类型
- 标准库类型的赋值运算符的引用限定符
- 错误 C2679:二进制"<<":未找到采用类型 'std::string_view' 的右侧操作数的运算符(或者没有可接受的转换)
- 如何确保接受的C++模板类型使运算符重载?
- C2678 二进制 '==':未找到采用 'Card' 类型左操作数的运算符(或者没有可接受的转换)
- C2679 二进制"<<":未找到采用右操作数类型的运算符
- 使用类型id运算符的最佳替代方法
- c++ 错误:二进制"运算符+"类型"矩阵*"和"矩阵*"的操作数无效
- C 中的运算符(::类型*)0表示什么意思
- 从类模板中排除转换运算符...<类型名 T>基于 T 上的特征
- 初始化中的模板转换运算符类型推导规则是什么?
- 重载运算符类型
- clang和gcc中的模板转换运算符类型推导不同
- 将运算符类型转换为除某些引用之外的任何算术类型
- 返回运算符类型=-引用或值
- VS 2012中的条件运算符类型转换
- 获取CXCursor_BinaryOperator的运算符类型