constant references

L

Leslaw Bieniasz

Cracow, 13.03.2008

Hi,

Assuming that I want to pass a reference to a function, and
make sure that the function will not make any changes to the object
passed, is it necessary to declare two "const's" or only one, i.e.
for example:

void fun(const Type& const x);

or

void fun(const Type& x);

Also, is there a difference between

const Type& const x

and

const Type const & x


L. Bieniasz
 
K

Kai-Uwe Bux

Leslaw said:
Cracow, 13.03.2008

Hi,

Assuming that I want to pass a reference to a function, and
make sure that the function will not make any changes to the object
passed, is it necessary to declare two "const's" or only one, i.e.
for example:

void fun(const Type& const x);

should not compile.
or

void fun(const Type& x);

should compile
Also, is there a difference between

const Type& const x

and

const Type const & x

not really: both are ill-formed, though in different ways. The first applies
a cv-qualifier to a reference type and the second violates [7.1.5/1].


Best

Kai-Uwe Bux
 
J

Joe Greer

Cracow, 13.03.2008

Hi,

Assuming that I want to pass a reference to a function, and
make sure that the function will not make any changes to the object
passed, is it necessary to declare two "const's" or only one, i.e.
for example:

void fun(const Type& const x);

This is illegal because references aren't objects and therefore can't be
const by definition (can't modify a non-existant thing in the first
place).
or

void fun(const Type& x);

This is generally sufficient.
Also, is there a difference between

const Type& const x

and

const Type const & x

Yes, they are both illegal in different ways. The first tries to make
something which isnt an object const and the second has two const
specifications for Type.

joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top