带const的模板函数参数

Lau*_*ura 2 c++ data-structures

我知道参考不是点.引用不是对象.所以没有const引用.

const int ci = 1024;
const int &rl = ci;
Run Code Online (Sandbox Code Playgroud)

我们不能写int const &rl = ...但是今天,我学习二叉树源代码:

template <typename T> //take e as the node insert the left child of the tree.
BinNodePosi(T) BinNode<T>::insertAsLC(T const & e) {  return lChild = new BinNode(e, this);  }
Run Code Online (Sandbox Code Playgroud)

我对它的参数感到困惑:T const&e.为什么不是const T&e?我认为后者是对的..也许我的问题太简单了,不能在这里发布.与数学不同,研究水平和学习水平分别有两个方面:MathOverFlow和math.stackexchange ..我找不到stackoverflow的初级网站..所以我在这里发布.请原谅我.非常感谢.

Pot*_*ter 5

const适用于它左边的东西,除非它是类型说明符序列中的第一个项目,在这种情况下它适用于下一个项目.

所以,T const & e完全等同于const T & e.你似乎在考虑的非法案件是T & const e.同样,将声明一个指向可变对象的不可变指针T * const e.

就个人而言,我避免const先放,因为这是语法的一个特例.