473,836 Members | 1,549 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 2711
* 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
5070
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
1980
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
2361
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
1357
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
1830
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
2295
by: quantdev2004 | last post by:
Hi all, I have been deling with this kind of code: class Foo { public: void NonConstMethod() {} };
34
31339
by: Perro Flaco | last post by:
Hi! I've got this: string str1; char * str2; .... str1 = "whatever"; .... str2 = (char *)str1.c_str();
16
3169
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
8307
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
6193
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
9825
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
9671
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10558
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10600
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10257
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
9387
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
4459
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
4022
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3116
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.