C++ 从属名称解析&;命名空间标准/标准库
在回答(更好地阅读)时,我提出了以下解决方案来解析运算符的从属名称: [temp.dep.res]/1: 在解析依赖名称时,将考虑来自以下来源的名称:C++ 从属名称解析&;命名空间标准/标准库,c++,templates,c++11,token-name-resolution,C++,Templates,C++11,Token Name Resolution,在回答(更好地阅读)时,我提出了以下解决方案来解析运算符的从属名称: [temp.dep.res]/1: 在解析依赖名称时,将考虑来自以下来源的名称: 在模板定义点可见的声明 来自与实例化上下文(14.6.4.1)和定义上下文中的函数参数类型相关联的名称空间的声明 当然,您也可以使用自己的对类型,只要该解决方案在自定义操作符>>的命名空间中引入了一个关联的类,这里的问题是对操作符>>进行调用的点在std命名空间的某个地方,参数类型所在的名称空间是std 如果编译器可以在调用发生的名称空间或参
- 在模板定义点可见的声明
- 来自与实例化上下文(14.6.4.1)和定义上下文中的函数参数类型相关联的名称空间的声明
当然,您也可以使用自己的
对
类型,只要该解决方案在自定义操作符>>
的命名空间中引入了一个关联的类,这里的问题是对操作符>>
进行调用的点在std
命名空间的某个地方,参数类型所在的名称空间是std
如果编译器可以在调用发生的名称空间或参数类型所在的名称空间中找到运算符>>
(在这种情况下,两者都是std
名称空间),无论重载解析是否可行(在名称查找之后执行),它不需要在父名称空间中查找更多的操作符>
重载
很遗憾,您的
运算符>>
位于全局命名空间中,因此找不到。您能提供一个参考吗?:)我很想在StandardOk中找到它:)[basic.lookup.unqual]/1;关联名称空间中的查找/依赖参数的查找在这里不起作用,因为这两种类型都来自名称空间std
。好吧,唯一关联的名称空间是名称空间std
,我不想在其中插入运算符。@DyP:哦,好吧,我误解了你的句子
#include <iostream>
#include <utility>
// this operator should be called from inside `istream_iterator`
std::istream& operator>>(std::istream& s, std::pair<int,int>& p)
{
s >> p.first >> p.second;
return s;
}
// include definition of `istream_iterator` only after declaring the operator
// -> temp.dep.res/1 bullet 1 applies??
#include <iterator>
#include <map>
#include <fstream>
int main()
{
std::ifstream in("file.in");
std::map<int, int> pp;
pp.insert( std::istream_iterator<std::pair<int, int>>{in},
std::istream_iterator<std::pair<int, int>>{} );
}
#include <iostream>
#include <utility>
// can include <iterator> already here,
// as the definition of a class template member function
// is only instantiated when the function is called (or explicit instantiation)
// (make sure there are no relevant instantiations before the definition
// of the operator>> below)
#include <iterator>
struct my_int
{
int m;
my_int() : m() {}
my_int(int p) : m(p) {}
operator int() const { return m; }
};
// this operator should be called from inside `istream_iterator`
std::istream& operator>>(std::istream& s, std::pair<my_int,my_int>& p)
{
s >> p.first.m >> p.second.m;
return s;
}
#include <map>
#include <fstream>
int main()
{
std::ifstream in("file.in");
std::map<int, int> pp;
pp.insert( std::istream_iterator<std::pair<my_int, my_int>>{in},
std::istream_iterator<std::pair<my_int, my_int>>{} );
}