我知道当我们在另一个模板中使用模板时,我们应该这样写:
vector<pair<int,int> > s;
如果我们在没有空格的情况下写它:
vector<pair<int,int>> s;
我们会收到一个错误:
嵌套模板参数列表中的`>>'应该是`>>'
我觉得这是可以理解的,但我不禁想知道,在哪种情况下,这真的很模糊?
Java 10附带了新的局部变量类型推断.令牌var可用于减少声明变量时所需的样板.例如
var s = "hello";
Run Code Online (Sandbox Code Playgroud)
根据什么类型的令牌在Java 10中完全是"var"?这个新令牌不是"关键字",而是"保留类型名称".因此,单词"var"仍然可以用作变量名,它保持与现有代码的向后兼容性.
var var = "you can do this";
Run Code Online (Sandbox Code Playgroud)
当在Java 9中引入"模块"功能时,这个新令牌的类型(以及其他9个相关令牌)被称为"受限制的关键字".也就是说,它们仅被视为特定上下文特定限制下的关键字.例如,您仍然可以使用称为模块的变量.
当新的语言功能以不会破坏现有用户定义符号的方式添加到C++时,它们被称为"上下文相关关键字".
varJava 10中新的"保留类型名称" 标记与"上下文相关关键字"或"受限制关键字" 之间是否存在概念差异.也就是说,var在某些特定于上下文的限制下,新令牌不仅仅是一个关键字.如果是这种情况,为什么不将它简单地添加到"受限制的关键字"列表中?
为了进一步增加我的混淆,JLS的当前草案版本说:
字符序列var通常被视为标识符,但在某些特殊情况下,其行为就像是关键字一样.
这个定义听起来像是一个"受限制的关键字".
我一直在阅读关于>>嵌套模板和>>移位运算符的结束...
现在我已经在我的MSVS2010中尝试过了,没有问题.
std::map<int, std::pair<int, int>> m;
这段代码正是我想要的(对的映射),但我应该得到一些错误 >>
这些天编译器更聪明?
我尝试用普通的std :: list编译一些自定义分配器的代码时发现了一个奇怪的行为.请考虑以下代码:
std::list<int, std::allocator<int>> theList;
theList.push_back(1);
Run Code Online (Sandbox Code Playgroud)
这是一个正常的列表变量,添加了一些数据.现在,如果我切换到:
std::list<int, std::allocator<int>> theList(std::allocator<int>());
theList.push_back(1);
Run Code Online (Sandbox Code Playgroud)
Visual Studio 2012无法使用"错误C2228:'.push_back'的左侧必须具有类/结构/联合"来编译它.当然std :: list有一个构造函数,它对分配器进行const引用.但如果我把它改为:
std::list<int, std::allocator<int>> theList = std::list<int, std::allocator<int>>(std::allocator<int>());
theList.push_back(1);
Run Code Online (Sandbox Code Playgroud)
一切都很好.为什么第二部分失败了?为了增加这种情况的陌生感,当试图按值返回List时:
typedef std::list<int, std::allocator<int>> TempType;
TempType func()
{
TempType theList(std::allocator<int>());
return theList;
}
Run Code Online (Sandbox Code Playgroud)
我得到"错误C2664:'std :: list <_Ty,_Alloc> :: list(const std :: list <_Ty,Alloc>&)':无法从'TempType( _cdecl*)转换参数1(std :: allocator) < Ty>( _ cdecl*)(void))'to'const std :: list <_Ty,_Alloc>&'".看起来编译器将列表视为函数声明.知道为什么会这样吗?