c – 为什么编译器找不到这个运算符<< overload?
发布时间:2020-12-16 09:47:29 所属栏目:百科 来源:网络整理
导读:我正在尝试编写运算符的重载对于将存储在 boost::variant 中的标准库容器的特定实例化.这是一个说明问题的小例子: #include iostream#include vectorstd::ostream operator( std::ostream os,const std::vector int ) { os "Streaming out std::vector int
我正在尝试编写运算符的重载<<对于将存储在
boost::variant 中的标准库容器的特定实例化.这是一个说明问题的小例子:
#include <iostream> #include <vector> std::ostream & operator<<( std::ostream & os,const std::vector< int > & ) { os << "Streaming out std::vector< int >"; return os; } std::ostream & operator<<( std::ostream & os,const std::vector< double > & ) { os << "Streaming out std::vector< double >"; return os; } #include <boost/variant.hpp> typedef boost::variant< std::vector< int >,std::vector< double > > MyVariant; int main( int argc,char * argv[] ) { std::cout << MyVariant(); return 0; } Clang的第一个错误是 boost/variant/detail/variant_io.hpp:64:14: error: invalid operands to binary expression ('std::basic_ostream<char>' and 'const std::vector<int,std::allocator<int>>') out_ << operand; ~~~~ ^ ~~~~~~~ 我意识到#include< boost / variant.hpp>在一个奇怪的地方.我很确定问题与模板中的两阶段名称查找有关,所以我移动了#include以尝试从clang documentation on lookup实现修复#1.修复#2来自该文档并不是一个好选择,因为我相信添加我的重载运算符<<到std命名空间会导致未定义的行为. 不应该在#include允许编译器找到定义之前定义我的运算符<< s?这个技术似乎适用于以下示例,改编自同一个clang页面. #include <iostream> namespace ns { struct Data {}; } std::ostream& operator<<(std::ostream& out,const ns::Data & data) { return out << "Some data"; } namespace ns2 { template<typename T> void Dump( std::ostream & out,const T & value) { out << value; } } int main( int argc,char * argv[] ) { ns2::Dump( std::cout,ns::Data() ); } 解决方法
在模板实例化期间,仅在阶段II查找期间找到取决于模板类型的功能模板.第二阶段查找不考虑在使用点可见的名称,而只考虑基于参数依赖查找找到的名称.由于std :: ostream和std :: vector< int>的唯一关联命名空间是namespace std它不会查找在全局命名空间中定义的输出运算符.当然,不允许将这些运算符添加到命名空间std中,这是一个真正的问题:您只能为容器定义这些运算符,至少包含一个用户定义类型!在这种限制的可能方法是添加一个自定义分配器,它只是从std :: allocator< T>中派生出来的.但它位于合适的用户定义命名空间中:然后您可以在此命名空间中定义输出运算符.这种方法的缺点是std :: vector< T> (即,没有allocator参数)几乎是词汇类型.
移动声明并没有帮助:第二阶段名称查找实际上并不依赖于声明的顺序,除了声明必须在实例化之前.唯一正确的解决方法是在阶段II查找中寻找名称空间中的运算符,这几乎意味着要打印的类型必须涉及用户定义的类型. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读