473,725 Members | 1,980 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
14 2692
In article <y%************ ********@news1. nokia.com>,
Risto Lankinen <rl******@hotma il.com> wrote:
"David White" <no@email.provi ded> wrote in message
news:43******@ news.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 */
}


That's an invalid overload, IOWs: you can't do that.
--
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 #11
In article <11************ *********@g43g2 000cwa.googlegr oups.com>,
mlimber <ml*****@gmail. com> wrote:
general principle of const-correctness is to make everything const
that can be const.


The problem is that can and should are different :)
--
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 #12

Greg Comeau wrote:
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.
--


I agree with mlimber point "to make everything const that can be
const".
People, who working with your code after you, may say "Thanks" for that.

Oct 14 '05 #13

"Greg Comeau" <co****@panix.c om> wrote in message
news:di******** **@panix3.panix .com...
In article <y%************ ********@news1. nokia.com>,
Risto Lankinen <rl******@hotma il.com> wrote:
"David White" <no@email.provi ded> wrote in message
news:43******@ news.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 */
}


That's an invalid overload, IOWs: you can't do that.


Illustrates the point, though, does it not?

If you indeed have hard time seeing the point, please re-read
but replace "bar" for "foo" in one of the functions.

- Risto -
Oct 14 '05 #14
In article <mO************ ********@news1. nokia.com>,
Risto Lankinen <rl******@hotma il.com> wrote:
"Greg Comeau" <co****@panix.c om> wrote in message
news:di******* ***@panix3.pani x.com...
In article <y%************ ********@news1. nokia.com>,
Risto Lankinen <rl******@hotma il.com> wrote:
>"David White" <no@email.provi ded> wrote in message
>news:43******@ news.eftel.com. ..
>> "Aleksey Loginov" <Al************ *@gmail.com> wrote in message
>> > it's makes difference when your const object behave not likeno-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 thefunction, >> not the original, that is declared const.
>
>void foo( test t )
>{
> t();
>}
>
>void foo( test const t )
>{
> t(); /* those terrible things now happen */
>}


That's an invalid overload, IOWs: you can't do that.


Illustrates the point, though, does it not?

If you indeed have hard time seeing the point, please re-read
but replace "bar" for "foo" in one of the functions.


This thread had a few "the point"s. If you think we disagree
somehow on one or more, just restate it instead of an editing session :)
--
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 14 '05 #15

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

Similar topics

5
5059
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
1967
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
2354
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
1351
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
1820
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
2287
by: quantdev2004 | last post by:
Hi all, I have been deling with this kind of code: class Foo { public: void NonConstMethod() {} };
34
31315
by: Perro Flaco | last post by:
Hi! I've got this: string str1; char * str2; .... str1 = "whatever"; .... str2 = (char *)str1.c_str();
16
3161
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
8302
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
6183
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
8888
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
8752
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
9401
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9174
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
9111
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
8096
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...
0
4517
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...
0
4782
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3221
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

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.