Page 1 of 1

c++ const reference question

Posted: Fri May 15, 2009 9:53 am
by orbitcowboy
Hi friends,

maybe it is a stupid question, but what is the difference between the two parameters of Foo1 and Foo2? Or, does it the same?

Code: Select all


template <typename T> T Foo1(const T & val)
{
	//...
	return val;
}

template <typename T> T Foo2(T const & val)
{
	//...
	return val;
}
Both functions have (to my mind) a constant reference parameter, which can't be modified. But what does it mean to write the const before or after the 'T' ?

Best regards and many thanks

Orbitcowboy

Posted: Fri May 15, 2009 10:59 am
by Romas
Hi,

const T& xx and T const& xx are the same. It is a matter of choise.

Posted: Fri May 15, 2009 3:36 pm
by mc2r
Romas wrote:Hi,

const T& xx and T const& xx are the same. It is a matter of choise.
Just to elaborate on Romas. There is no constant reference only references to constants as opposed to pointers where you can have a constant pointer or a pointer to a constant.

In the case of a reference as Romas says it is a matter of choice.

-Max