这个标题提到的问题涉及到C++标准在1990年左右引入模板时的设计决策。
为什么设计者使用尖括号<>
而不是圆括号()
?这样做可以避免许多程序员因位移相关错误而烦恼。
std::vector<std::vector<int>> // does not work until C++11
这个问题只在C++11中得到了修复。我不明白为什么要引入额外的语法,当使用圆括号可能会达到同样的效果,同时保持变化的最小化。相反,您可以使用
标签。template(typename T) // Define template if round brackets could be used
mytemplate { ... }
...
...
mytemplate(mytemplate(int)) obj; //Instantiate template when round brackets could be used
有没有精通 C++ 历史的人可以挖掘一下使用尖括号的最初设计理念?或者,您能否说明为什么其他解决方案同样适用不了呢?
int(int(int)) obj;
会使obj
成为一个指向返回int
并且接受一个指向返回int
并且接受int
的函数指针。或者类似于这样的东西。 - DeiDeitemplate_function(int())()
中,int()
是模板参数还是函数参数? - cpplearnerTemplatedType!(T)
,它没有二进制的!
,因此不存在C++中存在的歧义问题。 - Cubic