473,671 Members | 2,558 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

const correctness

Hi Guys,
I was going through a product's source code. They never use const
function arguments with default value.
for instance

Never noticed following type of declaration

foo ( const int param1, const bool = false) const;

instead
of this
foo ( const int param1, bool = false) const; is used widely.

I there any drawback of using default argument with const keyword?

Oct 13 '05 #1
14 2680
* ma***********@g mail.com:

I was going through a product's source code. They never use const
function arguments with default value.
for instance

Never noticed following type of declaration

foo ( const int param1, const bool = false) const;

instead
of this
foo ( const int param1, bool = false) const; is used widely.

I there any drawback of using default argument with const keyword?


No, the two declarations are equivalent (and by that I mean, they would
specify exactly the same function signature, if a return type was added).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 13 '05 #2
ma***********@g mail.com wrote:
Hi Guys,
I was going through a product's source code. They never use const
function arguments with default value.
for instance

Never noticed following type of declaration

foo ( const int param1, const bool = false) const;

instead
of this
foo ( const int param1, bool = false) const; is used widely.

I there any drawback of using default argument with const keyword?


I don't think there's any point in making any pass-by-value parameter const,
default or not. The const only affects how the function can use the
parameter and makes no difference to the caller. Why should the public
interface declare that a function will not modify its own private copy of an
argument? IMO such consts clutter up the public interface with junk that is
really none of the caller's business.

DW
Oct 13 '05 #3

David White wrote:
ma***********@g mail.com wrote:
Hi Guys,
I was going through a product's source code. They never use const
function arguments with default value.
for instance

Never noticed following type of declaration

foo ( const int param1, const bool = false) const;

instead
of this
foo ( const int param1, bool = false) const; is used widely.

I there any drawback of using default argument with const keyword?


I don't think there's any point in making any pass-by-value parameter const,
default or not. The const only affects how the function can use the
parameter and makes no difference to the caller. Why should the public
interface declare that a function will not modify its own private copy of an
argument? IMO such consts clutter up the public interface with junk that is
really none of the caller's business.


it's makes difference when your const object behave not like no-const:

struct test {
void operator () () { }
void operator () () const { /* terrible things here */ }
};

Oct 13 '05 #4
"Aleksey Loginov" <Al************ *@gmail.com> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
David White wrote:
I don't think there's any point in making any pass-by-value parameter const, default or not. The const only affects how the function can use the
parameter and makes no difference to the caller. Why should the public
interface declare that a function will not modify its own private copy of an argument? IMO such consts clutter up the public interface with junk that is really none of the caller's business.


it's makes difference when your const object behave not like no-const:

struct test {
void operator () () { }
void operator () () const { /* terrible things here */ }
};


I don't see how that relates to my post. If a function declares a
pass-by-value parameter const, you are free to pass either a const or
non-const value to it, since it's only the copy received by the function,
not the original, that is declared const.

DW

Oct 13 '05 #5

"David White" <no@email.provi ded> wrote in message
news:43******@n ews.eftel.com.. .
"Aleksey Loginov" <Al************ *@gmail.com> wrote in message

it's makes difference when your const object behave not like no-const:

struct test {
void operator () () { }
void operator () () const { /* terrible things here */ }
};


I don't see how that relates to my post. If a function declares a
pass-by-value parameter const, you are free to pass either a const or
non-const value to it, since it's only the copy received by the function,
not the original, that is declared const.


void foo( test t )
{
t();
}

void foo( test const t )
{
t(); /* those terrible things now happen */
}

Oct 13 '05 #6

"Risto Lankinen" <rl******@hotma il.com> wrote in message
news:y%******** ************@ne ws1.nokia.com.. .

"David White" <no@email.provi ded> wrote in message
news:43******@n ews.eftel.com.. .
"Aleksey Loginov" <Al************ *@gmail.com> wrote in message

it's makes difference when your const object behave not like no-const:

struct test {
void operator () () { }
void operator () () const { /* terrible things here */ }
};


I don't see how that relates to my post. If a function declares a
pass-by-value parameter const, you are free to pass either a const or
non-const value to it, since it's only the copy received by the function, not the original, that is declared const.


void foo( test t )
{
t();
}

void foo( test const t )
{
t(); /* those terrible things now happen */
}


Well, that looks like a completely contrived and unrealistic example to me.
The only times I've ever had const and non-const versions of the same
function are when they do exactly the same thing with the exception of
const, e.g.,

class Point
{
public:
const double &X() const { return x; }
double &X() { return x; }
// ...
private:
double x;
// ...
};

In any case, you still don't have to make your parameter const:
void foo( test t )
{
const test &u = t;
u();
}

I maintain that what goes on inside foo is of no concern to the caller, and
so should not manifest itself in the declaration of the parameter the caller
sees. After all, the parameter will cease to exist once the function
returns.

DW

Oct 13 '05 #7
David White wrote:
[snip]
I maintain that what goes on inside foo is of no concern to the caller, and
so should not manifest itself in the declaration of the parameter the caller
sees. After all, the parameter will cease to exist once the function
returns.


I agree with your point about cluttering up the interface, but I do
think that const pass-by-value parameters are useful. First, note that
one needn't put the const in the function prototype or class
declaration in order to use it in the definition since, as Alf notes
above, the const doesn't change the function signature:

struct Foo { Foo( int ); };

Foo::Foo( const int param )
{
const int magicNo = 42;
// ...
}

Applying const to param in the definition is no different than applying
it to the local constant magicNo, but making them both const makes the
function "easier to understand, track, and reason about" (_C++ Coding
Standards_, p. 30) because the programmer's intention is clear -- viz.,
neither magicNo or param will change throughout the function.

Of course there are cases where one might not want to make all
pass-by-value parameters const, but the general principle of
const-correctness is to make everything const that can be const. I
would apply that to pass-by-value parameters as well.

Cheers! --M

Oct 13 '05 #8
In article <11************ **********@g49g 2000cwa.googleg roups.com>,
<ma***********@ gmail.com> wrote:
I was going through a product's source code. They never use const
function arguments with default value.
for instance

Never noticed following type of declaration

foo ( const int param1, const bool = false) const;

instead
of this
foo ( const int param1, bool = false) const; is used widely.

I there any drawback of using default argument with const keyword?


In and of itself, that should be neutral.

You will find shops/people who don't allow const at the
top level in general, as they take the signature, and hence
interface of the functions very seriously, and so feel that
the const has no place at the top level. So one issue is
philosphical/conceptual lieing ;) But it can lead to physical
confusion, for instance:

void foo(const int arg);

Turns out that the const is tossed in the example,
since it is only a declaration. However, it would not
be tossed in the definition:

void foo(const int arg) { /* ... */ }

hence coming full circle to the opponents of such use.

Personally I don't like having to declare something only
then to make a const copy of it, or just ignore it (leaving
it non-const), but the above make sense too.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Oct 13 '05 #9
In article <11************ **********@g43g 2000cwa.googleg roups.com>,
Aleksey Loginov <Al************ *@gmail.com> wrote:
David White wrote:
ma***********@g mail.com wrote:
> I was going through a product's source code. They never use const
> function arguments with default value.
> for instance
>
> Never noticed following type of declaration
>
> foo ( const int param1, const bool = false) const;
>
> instead
> of this
> foo ( const int param1, bool = false) const; is used widely.
>
> I there any drawback of using default argument with const keyword?


I don't think there's any point in making any pass-by-value parameter const,
default or not. The const only affects how the function can use the
parameter and makes no difference to the caller. Why should the public
interface declare that a function will not modify its own private copy of an
argument? IMO such consts clutter up the public interface with junk that is
really none of the caller's business.


it's makes difference when your const object behave not like no-const:

struct test {
void operator () () { }
void operator () () const { /* terrible things here */ }
};


David is talking about const applied as a top level qualified,
not a const member fucnction. I realize you might be trying
to point of the benefits of const though; I doubt David
disagrees about their benefits, just how they sh/c/ould come about.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Oct 13 '05 #10

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

Similar topics

5
5055
by: Bolin | last post by:
Hi all, A question about smart pointers of constant objects. The problem is to convert from Ptr<T> to Ptr<const T>. I have look up and seen some answers to this question, but I guess I am too stupid to understand and make them work. E.g. I have read that boost's smart pointers are able to do this convertion, but the following code doesn't compile (VC++6.0):
3
1961
by: ded' | last post by:
Hello ! I've read in a magazine "reference parameter in operator= must be const, because in C++, temporary objects are const" and then my operator would not work with temporary objets. But, my compiler doesn't have temporary const objects. Are there any reasons to have a const reference parameter ? Thanks in advance for your help
2
2347
by: Jim Strathmeyer | last post by:
I have a weird question about const correctness when using an stl list. I have a wrapper Inventory class that holds a list of pointers to Items. (Yes, they have to be pointers.) Now, obviously the Inventory class isn't going to mutate the Items, so its Add function should be Add(const Item *), and the list should be std::list<const Item *>. One way to access the Inventory's item's is to iterate through them with: std::list<const Item...
2
1348
by: Jianli Shen | last post by:
in a *.h file, there is a declaration: const ClassName *functionName() const {return oneVar;} I was confused by the two const there. can anybody help explain why we need the first const. why we need the second const here ? Thanks
11
1816
by: Markus.Elfring | last post by:
A couple of software provides const-incorrect programming interfaces. I guess that it is possible to develop const-correct APIs/SDKs from the beginning if a few basic design rules and patterns would be considered. How difficult is it for beginners to make it right and to get used to this kind of programming style? How much do you need to fiddle with "const_cast" because the key word "const" was forgotten for type specifiers in important...
10
2279
by: quantdev2004 | last post by:
Hi all, I have been deling with this kind of code: class Foo { public: void NonConstMethod() {} };
34
31288
by: Perro Flaco | last post by:
Hi! I've got this: string str1; char * str2; .... str1 = "whatever"; .... str2 = (char *)str1.c_str();
16
3156
by: hzmonte | last post by:
Correct me if I am wrong, declaring formal parameters of functions as const, if they should not be/is not changed, has 2 benefits; 1. It tells the program that calls this function that the parameter will not be changed - so don't worry. 2. It tells the implementor and the maintainer of this function that the parameter should not be changed inside the function. And it is for this reason that some people advocate that it is a good idea to...
6
8299
by: Spoon | last post by:
Hello, I don't understand why gcc barks at me in this situation: $ cat foo.c extern void func(const int * const list, int nent); int main(void) { int *p;
4
6181
by: Giovanni Gherdovich | last post by:
Hello, I'm doing some toy experiments to see how the algoritm std::transform and the function adapter std::bind2nd can play together, but my compiler give my the error error: passing `const traslate' as `this' argument of `circle traslate::operator()(circle, std::vector<double, std::allocator<double)'
0
8485
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8677
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7446
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6238
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5704
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4227
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2819
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2062
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.