Reputation: 209
I know reference is not like point. reference is not an object. so there is no const reference.
const int ci = 1024;
const int &rl = ci;
we can't write int const &rl = ...
But today ,I am learn a binary tree source code:
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); }
I am confused with its parameter: T const & e . why not const T& e? I think the latter is right.. Maybe my question is too simple to post here. Unlike mathematics ,there are two side for Research level and study level respectively : MathOverFlow and math.stackexchange .. I can't find the junior site for stackoverflow.. So I post it here. Please forgive me. Thanks very much.
Upvotes: 2
Views: 88
Reputation: 137930
const
applies to the thing immediately to its left, unless it is the first item in the type specifier sequence, in which case it applies to the next item.
So, T const & e
is exactly equivalent to const T & e
. The illegal case you seem to be thinking of is T & const e
. Likewise, an immutable pointer to a mutable object would be declared T * const e
.
Personally, I avoid putting const
first because it is a special case of the grammar.
Upvotes: 5