C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

reference-to-const

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
karl
Guest





PostPosted: Thu Oct 30, 2003 10:12 am    Post subject: reference-to-const Reply with quote



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
Back to top
Andrey Tarasevich
Guest





PostPosted: Thu Oct 30, 2003 10:15 am    Post subject: Re: reference-to-const Reply with quote



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

and

template foo(const T& p);

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

No difference at all.

--
Best regards,
Andrey Tarasevich


Back to top
Mark Kerns
Guest





PostPosted: Thu Oct 30, 2003 1:18 pm    Post subject: Re: reference-to-const Reply with quote



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

and

template foo(const T& p);

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

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.



Back to top
Joshua Boelter
Guest





PostPosted: Thu Oct 30, 2003 4:34 pm    Post subject: Re: reference-to-const Reply with quote

Mark Kerns wrote:
Quote:
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.


Back to top
Andrey Tarasevich
Guest





PostPosted: Thu Oct 30, 2003 5:12 pm    Post subject: Re: reference-to-const Reply with quote

Joshua Boelter wrote:
Quote:
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:


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.

--
Best regards,
Andrey Tarasevich


Back to top
Ron Natalie
Guest





PostPosted: Thu Oct 30, 2003 5:13 pm    Post subject: Re: reference-to-const Reply with quote


"Joshua Boelter" <joshuaDOTboelter (AT) intel (DOT) com> wrote


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

Really? How so?

Quote:

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?



Back to top
Mark Kerns
Guest





PostPosted: Thu Oct 30, 2003 6:17 pm    Post subject: Re: reference-to-const Reply with quote

Quote:
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 :)



Back to top
Rolf Magnus
Guest





PostPosted: Fri Oct 31, 2003 1:03 am    Post subject: Re: reference-to-const Reply with quote

Joshua Boelter wrote:

Quote:
Mark Kerns wrote:
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?

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


Back to top
Micah Cowan
Guest





PostPosted: Fri Oct 31, 2003 8:48 am    Post subject: Re: reference-to-const Reply with quote

Joshua Boelter <joshuaDOTboelter (AT) intel (DOT) com> writes:

Quote:
Mark Kerns wrote:
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 ?

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".

Quote:
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.

--
Micah J. Cowan
[email]micah (AT) cowan (DOT) name[/email]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.