reference-to-const

K

karl

hi all,

could anyone explain the differences between:


template<typename T> // let's suppose that T could be a user - defined class
foo(T const & p);

and

template<typename T>
foo(const T& p);

in terms of use, efficiency, use of temporary objects and so on...

thanks
 
A

Andrey Tarasevich

karl said:
...
template<typename T> // let's suppose that T could be a user - defined class
foo(T const & p);

and

template<typename T>
foo(const T& p);

in terms of use, efficiency, use of temporary objects and so on...
...

No difference at all.
 
M

Mark Kerns

template said:
No difference at all.

(To the op): He means that "const" can appear before or after the type at
your discretion (choice). It's optional in the language and has no bearing
at runtime whatsoever. At compile time it's possible there could be a
performance difference depending on the parsing techniques used but it would
likely be so negligible that no programmer should ever worry about it (and
in general there are standard algorithms for parsing declarators anyway).
BTW, most generally use:

const T &p

but some like:

T const &p

typically because it can be read from right to left as in "p" is "&"
(reference) to a "const" object of type "T". However, reading the first
example becomes second nature very quickly so the latter argument is hollow
IMO.
 
J

Joshua Boelter

Mark said:
typically because it can be read from right to left as in "p" is "&"
(reference) to a "const" object of type "T". However, reading the first
example becomes second nature very quickly so the latter argument is hollow
IMO.

Templates are one good reason to put your const to the right:

typedef char* CHARPTR;

const CHARPTR p;

What is p?
pointer to const char?
const pointer to char?
const pointer to const char?

Or this:

template < typename T >
struct widget
{
const T p;
};

What is widget< char * >::p ?


for the typedef

const CHARPTR p;
CHARPTR const p;
char * const p;

for the typename

const T p;
T const p;
char * const p;

In both cases p is a const pointer to char.

Joshua Boelter
These are my opinions not official opinions of Intel Corp.
 
A

Andrey Tarasevich

Joshua said:
Templates are one good reason to put your const to the right:

Sorry, but I don't see how your code samples illustrate that const
should be put "to the right". It has always been a matter of personal
preference. Templates (and/or typedefs) don't change anything here.
 
R

Ron Natalie

Joshua Boelter said:
Templates are one good reason to put your const to the right:

Really? How so?
typedef char* CHARPTR;

const CHARPTR p;

What is p?
pointer to const char?
const pointer to char?
const pointer to const char?

To anybody who knows C++, it's obvious. Typedef and template
are not just text subsitution. The rules for the declarations are
consistant:

const CHARPTR p;
const T p;

In either case p, is a const instance of whatever T or CHARPTR is.

What's hard about it, and how does putting the const after the rest of
the type name make any difference on the understanding?
 
M

Mark Kerns

Templates are one good reason to put your const to the right:

Your examples aren't typical of most real-world code but the point is
well-taken in spite of the objections of the others. That is, for an example
such as:

const CHARPTR p;
CHARPTR const p;

The rules certainly don't change but a less experienced developer may not be
able to get his mind around this as quickly as an experienced developer (who
won't be tripped up by this at all). This type of obfuscating code violates
the KISS principle in general however and should generally be avoided IMO.
When it is used though (I have seen it before) no programmer who knows what
he's doing will misunderstand it so your point is noted though I consider it
very weak :)
 
R

Rolf Magnus

Joshua said:
Templates are one good reason to put your const to the right:

typedef char* CHARPTR;

const CHARPTR p;

What is p?
pointer to const char?
const pointer to char?
const pointer to const char?

If at all, I'd actually see this as one of the reasons to avoid hiding
pointers behind typedefs.
 
M

Micah Cowan

Joshua Boelter said:
Templates are one good reason to put your const to the right:

typedef char* CHARPTR;

const CHARPTR p;

What is p?
pointer to const char?
const pointer to char?
const pointer to const char?

Or this:

template < typename T >
struct widget
{
const T p;
};

What is widget< char * >::p ?

const pointer to char. This is easy to realize once you realize
that the types denoted by typenames or type aliases (typedefs)
can't be messed with "on the inside", you can only modify them
"externally".
for the typedef

const CHARPTR p;
CHARPTR const p;
char * const p;

for the typename

const T p;
T const p;
char * const p;

In both cases p is a const pointer to char.

Right. So what difference does it make whether the const is to
the right or to the left? It's stylistic: some people find one
way more readable, others another, and some find them both
equally legible.
 

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

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,372
Latest member
LucretiaFo

Latest Threads

Top