为什么编译器找不到这个运算符<< overload?

Rec*_*ent 3 c++ boost operator-overloading variant argument-dependent-lookup

我正在尝试为operator<<标准库容器的特定实例化编写重载,这些容器将存储在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;
}
Run Code Online (Sandbox Code Playgroud)

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;
        ~~~~ ^  ~~~~~~~
Run Code Online (Sandbox Code Playgroud)

我意识到这#include <boost/variant.hpp>是一个奇怪的地方.我很确定这个问题与模板中的两阶段名称查找有关,因此我#include试图在查找clang文档中实现修复#1 .修复该文档中的#2不是一个好选择,因为我相信将重载的operator <<添加到std命名空间会导致未定义的行为.

operator<<#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() );
}
Run Code Online (Sandbox Code Playgroud)

Die*_*ühl 7

在模板实例化期间,仅在阶段II查找期间找到取决于模板类型的功能模板.第二阶段查找不考虑在使用点可见的名称,而只考虑基于参数依赖查找找到的名称.由于且唯一关联的命名空间是std::ostreamstd::vector<int>命名空间,std因此它不会查找在全局命名空间中定义的输出操作符.当然,您不允许将这些运算符添加到名称空间中std,这是一个真正的问题:您只能为容器定义这些运算符,至少包含一个用户定义类型!在可能的方面,这个限制是添加一个简单派生自定义分配器std::allocator<T>但它位于合适的用户定义命名空间中:然后您可以在此命名空间中定义输出运算符.这种方法的缺点是std::vector<T>(即没有分配器参数)几乎是词汇类型.

移动声明并没有帮助:第二阶段名称查找实际上并不依赖于声明的顺序,除了声明必须在实例化之前.唯一正确的解决方法是在阶段II查找中寻找名称空间中的运算符,这几乎意味着要打印的类型必须涉及用户定义的类型.

  • @TemplateRex:同意....使用C++ 11,可以通过使用`template <typename T> struct myvector:std :: vector <int> {using std :: vector <T> :: vector; ```using`声明导致`std :: vector <T>`的构造函数被继承. (2认同)