473,698 Members | 2,668 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

const on primitive type parameters

Anybody have any preferences as to declaring primitive type parameter as
const, if the function body doesn't change it?

e.g.:

int f(int x)
{
return x+2;
}

versus

int f(const int x)
{
return x + 2;
}

I'm trying to update a project-wide style guide, and there are
developers who are insistent about the second style.
Jun 22 '06 #1
9 4011
red floyd wrote:
Anybody have any preferences as to declaring primitive type parameter
as const, if the function body doesn't change it?

e.g.:

int f(int x)
{
return x+2;
}

versus

int f(const int x)
{
return x + 2;
}

I'm trying to update a project-wide style guide, and there are
developers who are insistent about the second style.


Have you tried asking them?

To asnwer your question, I prefer the latter. Adding a 'const' there
changes nothing for a function like that, except requires 6 characters
to be typed.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 22 '06 #2
red floyd wrote:
Anybody have any preferences as to declaring primitive type parameter as
const, if the function body doesn't change it?

e.g.:

int f(int x)
{
return x+2;
}

versus

int f(const int x)
{
return x + 2;
}

I'm trying to update a project-wide style guide, and there are
developers who are insistent about the second style.


I'd say that it would make more sense if the logic was "reversed". I
have seen some style guides that suggested that function parameter
values should never be changed inside the function. Whenever it is
necessary to "play" with the value, it should be copied to a local
variable first. I don't follow this rule myself, but I'd agree that it
makes certain sense. For a programmer who follows such style guide, it
would be perfectly logical to _always_ declare all function parameters
as 'const' (apart from situations when it is impossible or makes no
sense, like references and arrays).

The logic that you propose ("declare it as const iff it is not changed")
doesn't appear to be very useful to me. Maybe it is just me, though...

--
Best regards,
Andrey Tarasevich
Jun 22 '06 #3
red floyd posted:
Anybody have any preferences as to declaring primitive type parameter as const, if the function body doesn't change it?

e.g.:

int f(int x)
{
return x+2;
}

versus

int f(const int x)
{
return x + 2;
}

I'm trying to update a project-wide style guide, and there are
developers who are insistent about the second style.

I use the second whenever I won't be altering the object. However,
there's times when I do the likes of the following:

void Func( char *p )
{
for( ; *p; ++p ) *p = 'a';
}

Jun 22 '06 #4
On Thu, 22 Jun 2006 17:11:06 GMT, red floyd <no*****@here.d ude> wrote:
Anybody have any preferences as to declaring primitive type parameter as
const, if the function body doesn't change it?

e.g.:

int f(int x)
{
return x+2;
}

versus

int f(const int x)
{
return x + 2;
}
Changes to the copy of a value in the 'function body' are an
implementation detail that should not affect the interface. I see no
advantage for version 2.
I'm trying to update a project-wide style guide, and there are
developers who are insistent about the second style.


At least, the first version is written in the usual style.

Best wishes,
Roland Pibinger
Jun 22 '06 #5
Roland Pibinger wrote:
On Thu, 22 Jun 2006 17:11:06 GMT, red floyd <no*****@here.d ude> wrote:
Anybody have any preferences as to declaring primitive type parameter as
const, if the function body doesn't change it?

e.g.:

int f(int x)
{
return x+2;
}

versus

int f(const int x)
{
return x + 2;
}


Changes to the copy of a value in the 'function body' are an
implementation detail that should not affect the interface. I see no
advantage for version 2.
I'm trying to update a project-wide style guide, and there are
developers who are insistent about the second style.


At least, the first version is written in the usual style.


For once, Roland, I agree with you. I was overruled by TPTB on my
project however. If a function doesn't tweak a parameter, then it's
const. If it changes (as the loop example someone else posted does),
then it's non-const.

Ugh.
Jun 22 '06 #6
I V
On Thu, 22 Jun 2006 21:48:48 +0000, Roland Pibinger wrote:
Changes to the copy of a value in the 'function body' are an
implementation detail that should not affect the interface. I see no
advantage for version 2.


Does declaring a by-value argument const affect the interface, though?
Consider:

void f(int i)
{
return;
}

void f(const int i)
{
return;
}

G++ and Comeau, at least, complain that the second is a redefinition of
the function, which makes sense to me. If that's the correct behavior, it
then presumably the const-ness of the arguments is not part of the
function signature. Thus, when you're declaring the interface, you can
omit the const, but in the function definition, you add the const, if it's
appropriate, for the same reason you would declare a local variable const.
Jun 23 '06 #7
On Thu, 22 Jun 2006 22:18:34 -0700, I V <wr******@gmail .com> wrote:
On Thu, 22 Jun 2006 21:48:48 +0000, Roland Pibinger wrote:
Changes to the copy of a value in the 'function body' are an
implementation detail that should not affect the interface. I see no
advantage for version 2.


Does declaring a by-value argument const affect the interface, though?
Consider:

void f(int i)
{
return;
}

void f(const int i)
{
return;
}

G++ and Comeau, at least, complain that the second is a redefinition of
the function, which makes sense to me. If that's the correct behavior, it
then presumably the const-ness of the arguments is not part of the
function signature. Thus, when you're declaring the interface, you can
omit the const, but in the function definition, you add the const, if it's
appropriate, for the same reason you would declare a local variable const.


You are right from the syntactical point of view. But shall the
following functions really be different WRT the constness of the
parameter?

int f1 (const int i) {
return i+2;
}

int f2 (int i) {
return i+=2;
}
Jun 23 '06 #8
red floyd wrote:
Roland Pibinger wrote:
On Thu, 22 Jun 2006 17:11:06 GMT, red floyd <no*****@here.d ude> wrote:
Anybody have any preferences as to declaring primitive type parameter as
const, if the function body doesn't change it?

e.g.:

int f(int x)
{
return x+2;
}

versus

int f(const int x)
{
return x + 2;
}


Changes to the copy of a value in the 'function body' are an
implementation detail that should not affect the interface. I see no
advantage for version 2.
I'm trying to update a project-wide style guide, and there are
developers who are insistent about the second style.


At least, the first version is written in the usual style.


For once, Roland, I agree with you. I was overruled by TPTB on my
project however. If a function doesn't tweak a parameter, then it's
const. If it changes (as the loop example someone else posted does),
then it's non-const.


I clearly prefer version 2. What can be const should be const. For trivial
examples like the one above it may not make a difference but for more
complex functions it improves code readability because you can rely on the
value of the variable not changing throughout the function. I actually try
to avoid updating variables to preserve their constness e.g. instead of

int f(int x)
{
// use x
++x;
// use x
}

I write

int f(const int x)
{
// use x
const int x1 = x + 1;
// use x1
}

also helpful is the ?: operator to initialize const variables.

I think this style of programming captures some of the advantages of (pure)
functional languages.

Jun 23 '06 #9
I V
On Fri, 23 Jun 2006 19:58:46 +0000, Roland Pibinger wrote:
You are right from the syntactical point of view. But shall the
following functions really be different WRT the constness of the
parameter?

int f1 (const int i) {
return i+2;
}

int f2 (int i) {
return i+=2;
}


Why not? Should these two function differ as to the constness of the local
variable?

int f1()
{
const int i = get_value();
return i+2;
}

int f2()
{
int i = get_value();
return i+=2;
}

Obviously, for small, pointless example functions it doesn't really
matter. But, if there are advantages to making local variables const,
surely the same advantages apply to making parameters const. After all a
parameter _is_ a local variable; some features of it are also part of the
interface, but constness isn't one of these, so wrt const, the same rules
should apply to parameters as to any other local variable, I would think.
Jun 23 '06 #10

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

Similar topics

4
3908
by: Marcin Vorbrodt | last post by:
I understand that compiler may optimize better, if as parameters to my functions i pass: const Object &o instead of Object &o All the implicit type conversion and all sounds good (Effective C++ rules).
20
2126
by: christopher diggins | last post by:
I have heard it is considered good practice to pass function parameters as const& as often as possible, is this true? Is it possible to go overboard? And if so why? Thanks a lot in advance everyone! Christopher Diggins http://www.cdiggins.com
3
2728
by: case2005 | last post by:
Can anyone help with the following, I don't know if it's possible, but I'm certain there must be a standard way of dealing with this. I have the following: template<typename FooBar, typename Foo> class Bar { private: Foo foo;
4
1718
by: BigMan | last post by:
Why does an emtpy class require an explicit default ctor in order to initialize const objects of that type: class EmptyClass { }; class EmptyClass2 { EmtpyClass2( ) { }
6
8301
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;
10
2783
by: d3x0xr | last post by:
---- Section 1 ---- ------ x.c int main( void ) { char **a; char const *const *b; b = a; // line(9)
8
4575
by: minseokoh | last post by:
Hi, Could someone explain why "const" is located after the function name and what it means? inline unsigned char *access(int off) const { if (off < 0) abort(); return (&bits_); }
6
5582
by: Darin Johnson | last post by:
I keep running across that I'm maintaining that likes to define function parameters as "const char &" or "const int &", etc. Ie, constant reference parameters to a primitive type. This is for normal functions, not operators. I keep changing these to just have the plain old type, which is more efficient (I'm using embedded systems) and less obtuse. I'm puzzled why this one programmer insisted on odd style everywhere. Maybe he's just...
28
2642
by: lovecreatesbeauty | last post by:
Is the keyword const necessary in the comparison function in qsort and bsearch? int (*compar)(const void *, const void *) If the pointer cannot be dereferenced why worry if the pointed object will be modified?
0
8674
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
9157
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...
0
9023
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
8893
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
8861
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
7721
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
6518
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
4366
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...
3
1999
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.