473,503 Members | 5,382 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Do you usually to pass a reference to set (or other stl container) asconst

void func(const set<string&);

It is typical for pointers, but not reference to pass as const if you
don't want modified. Who finds himself doing it above -- passing the
reference as const?

Aug 20 '08 #1
8 1540
On Aug 20, 4:53 pm, puzzlecracker <ironsel2...@gmail.comwrote:
void func(const set<string&);

It is typical for pointers, but not reference to pass as const if you
don't want modified. Who finds himself doing it above -- passing the
reference as const?
Its not typical.
Passing a reference to const should be the default.
The reason you often see references to mutables is due to bad habits
gained from pointers.

// bad
void func(const set<string>* p); // ptr is reseatable
// better
void func(const set<string>* const p); // const ptr to const set
// perfect
void func(const set<string>& r); // not reseatable, set is const
Aug 20 '08 #2
On 20 Aug, 23:14, Salt_Peter <pj_h...@yahoo.comwrote:
>
Its not typical.
Passing a reference to const should be the default.
The reason you often see references to mutables is due to bad habits
gained from pointers.

// bad
void func(const set<string>* p); // ptr is reseatable
// better
void func(const set<string>* const p); // const ptr to const set
// perfect
void func(const set<string>& r); // not reseatable, set is const
I must admit that I do not understand why your example of "bad" is so
bad. Isn't that just like saying:

void f(int i) // bad, the local copy of i can be modified
void f(const int i) // good

Which is simply false.

DP
Aug 21 '08 #3
On Aug 21, 1:34*am, Triple-DES <DenPlettf...@gmail.comwrote:
On 20 Aug, 23:14, Salt_Peter <pj_h...@yahoo.comwrote:
Its not typical.
Passing a reference to const should be the default.
The reason you often see references to mutables is due to bad habits
gained from pointers.
// bad
void func(const set<string>* p); // ptr is reseatable
// better
void func(const set<string>* const p); // const ptr to const set
// perfect
void func(const set<string>& r); // not reseatable, set is const

I must admit that I do not understand why your example of "bad" is so
bad. Isn't that just like saying:

void f(int i) // bad, the local copy of i can be modified
void f(const int i) // good

Which is simply false.

DP
I think void f(const int i) is plainly stupid....
Do you know the difference between

function(const T *p)
function (T const *p )
function (T * const p)
function (const T* const p)

and harder ones:

function(const T **p)
function(T **const p)
Aug 21 '08 #4
On 21 Aug, 14:45, puzzlecracker <ironsel2...@gmail.comwrote:
On Aug 21, 1:34*am, Triple-DES <DenPlettf...@gmail.comwrote:


On 20 Aug, 23:14, Salt_Peter <pj_h...@yahoo.comwrote:
Its not typical.
Passing a reference to const should be the default.
The reason you often see references to mutables is due to bad habits
gained from pointers.
// bad
void func(const set<string>* p); // ptr is reseatable
// better
void func(const set<string>* const p); // const ptr to const set
// perfect
void func(const set<string>& r); // not reseatable, set is const
I must admit that I do not understand why your example of "bad" is so
bad. Isn't that just like saying:
void f(int i) // bad, the local copy of i can be modified
void f(const int i) // good
Which is simply false.
DP

I think void f(const int i) *is plainly stupid....

Do you know the difference between

function(const T *p)
function (T const *p )
function (T * const p)
function (const T* const p)

and harder ones:

function(const T **p)
function(T **const p)
Yes, that's why I was interested to hear Salt_Peter's argumentation
why

f(T const * const p);

is "better" than:

f(T const * p)

DP
Aug 21 '08 #5
puzzlecracker wrote:
I think void f(const int i) is plainly stupid....
Why? It documents that the parameter is not modified in the program,
and additionally makes the compiler tell you if you break the promise
nevertheless. If it was not your intention to modify the parameter (even
though it's just a local copy), the compiler error can hint you at your
mistake.

Aug 21 '08 #6
On Thu, 21 Aug 2008 15:37:46 GMT, Juha Nieminen <no****@thanks.invalidwrote:
puzzlecracker wrote:
>I think void f(const int i) is plainly stupid....

Why? It documents that the parameter is not modified in the program,
Yes, but callers couldn't care less -- which is why C++ allows this:

void f(int i); // interface

void f(const int i) // implementation, locks down 'i'
{ ... }
and additionally makes the compiler tell you if you break the promise
nevertheless. If it was not your intention to modify the parameter (even
though it's just a local copy), the compiler error can hint you at your
mistake.
Yes, I use that a lot, if the function is messy enough.
It can be useful to know that 'i' still has the same value
two pages down in the code ...

/Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.se R'lyeh wgah'nagl fhtagn!
Aug 21 '08 #7
Jorgen Grahn wrote:
On Thu, 21 Aug 2008 15:37:46 GMT, Juha Nieminen
<no****@thanks.invalidwrote:
>puzzlecracker wrote:
>>I think void f(const int i) is plainly stupid....

Why? It documents that the parameter is not modified in the
program,

Yes, but callers couldn't care less -- which is why C++ allows this:

void f(int i); // interface

void f(const int i) // implementation, locks down 'i'
{ ... }
>and additionally makes the compiler tell you if you break the
promise nevertheless. If it was not your intention to modify the
parameter (even though it's just a local copy), the compiler error
can hint you at your mistake.

Yes, I use that a lot, if the function is messy enough.
It can be useful to know that 'i' still has the same value
two pages down in the code ...
Yes, I use that as well occationally, even though it is a confession
that the code is not really good enough.

The real solution is, of course, not to have two pages of code in the
function. :-)
Bo Persson
Aug 21 '08 #8
On Thu, 21 Aug 2008 21:21:10 +0200, Bo Persson <bo*@gmb.dkwrote:
Jorgen Grahn wrote:
>void f(int i); // interface

void f(const int i) // implementation, locks down 'i'
{ ... }
>>and additionally makes the compiler tell you if you break the
promise nevertheless. If it was not your intention to modify the
parameter (even though it's just a local copy), the compiler error
can hint you at your mistake.

Yes, I use that a lot, if the function is messy enough.
It can be useful to know that 'i' still has the same value
two pages down in the code ...

Yes, I use that as well occationally, even though it is a confession
that the code is not really good enough.

The real solution is, of course, not to have two pages of code in the
function. :-)
:-) It's actually something I use when cleaning up other people's[0]
messy functions: place "const" on anything which I suspect doesn't or
shouldn't change, and recompile. If it compiles, it becomes easier to
reason about the correctness of the code.

C++ is nice for cleanup work in general -- nicer than any other
language I use. There are so many things you can lock down by
applying 'const', by making things (e.g. copy constructors) private,
by wrapping enums and integers in classes). Runtime bugs pop up as
compiler warnings or errors.

/Jorgen

[0] Can't use it on my own messy code, because I apply const if I
suspect it will become messy.

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.se R'lyeh wgah'nagl fhtagn!
Aug 22 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1422
by: Shaun Marshall | last post by:
Hope someone can help, for an assignment i was given a starter application package, with that starter package i had to create some employee classes and test harnesses. below iv pasted my Container...
4
3212
by: Omar Llanos | last post by:
Recently, I posted a question on how to invoke a textbox control in Form1 (parent form) from within Form2 (which is inherited from Form1). Someone suggested to pass a reference of the Form1 to the...
3
1747
by: John E. | last post by:
I am trying to find a way to not have to reference an object in all my projects, since it is initialized & instantiated in my Common class. I have a 4 tier project (presentation, rules, dal,...
8
2381
by: toton | last post by:
HI, One more small doubt from today's mail. I have certain function which returns a pointer (sometimes a const pointer from a const member function). And certain member function needs reference...
4
2273
by: Jon Slaughter | last post by:
I'm reading a book on C# and it says there are 4 ways of passing types: 1. Pass value type by value 2. Pass value type by reference 3. Pass reference by value 4. Pass reference by reference. ...
29
3622
by: shuisheng | last post by:
Dear All, The problem of choosing pointer or reference is always confusing me. Would you please give me some suggestion on it. I appreciate your kind help. For example, I'd like to convert a...
2
1965
by: Daniel Lipovetsky | last post by:
I would like for an object to "report" to a container object when a new instance is created or deleted. I could have a container object that is called when a new instance is created, as below. ...
2
2073
by: OuaisBla | last post by:
What I'd like to do and what I tought possible was to have a deque of string when the string was a reference (not a pointer) Then I simply tried: std::deque<std::string const &> mFifo; But,...
0
840
by: Utku Altinkaya | last post by:
Hello, I am wrapping the reference container vector as document suggests. typedef std::vector<CUEEntity*EntityContainer; class_<CUEEntity>("Entity", init<CUEEntity*>()); ...
0
7193
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7067
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6975
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.