473,287 Members | 1,565 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

why "const char &" as a parameter?

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 applying a template
without thinking, as if it were like "const BigStruct &".
But I'm wondering if there's some school of thought out
there that encourages this and I'm missing something subtle.

--
Darin Johnson
Feb 23 '08 #1
6 5524
Darin Johnson wrote:
I keep changing these to just have the plain old type, which
is more efficient (I'm using embedded systems)
Did you check that your compiler is not already doing that
automatically for you?
Feb 23 '08 #2
Juha Nieminen wrote:
Darin Johnson wrote:
>I keep changing these to just have the plain old type, which
is more efficient (I'm using embedded systems)

Did you check that your compiler is not already doing that
automatically for you?
If the function is expanded inline, this is highly likely, but for other
functions OTOH, it's rather not likely.

Feb 23 '08 #3
Darin Johnson wrote:
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 applying a template
without thinking, as if it were like "const BigStruct &".
But I'm wondering if there's some school of thought out
there that encourages this and I'm missing something subtle.

--
Darin Johnson
It was already implicitly mentioned but before changing make sure the
functions do not use the address of the parameter; otherwise, the
behavior may change. Barring that, I am not aware of any drawbacks of
what you are doing -- as long as you know it gives your code an
advantage on your particular platform.

With the performance equal, I personally would still use by-value
convention because, for a prospective code reader, it results in less
tokens to scan and comprehend and less side effects to watch for.

If a parameter type is a template parameter, however, the whole
different can of worms should be considered. Unsure if it is relevant to
your question.

Hope this will help
-Pavel
Feb 23 '08 #4
On 2008-02-23 13:49:33, Pavel wrote:
Darin Johnson wrote:
>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 applying a template without thinking, as if it were like "const
BigStruct &". But I'm wondering if there's some school of thought out
there that encourages this and I'm missing something subtle.

It was already implicitly mentioned but before changing make sure the
functions do not use the address of the parameter; otherwise, the
behavior may change. Barring that, I am not aware of any drawbacks of
what you are doing -- as long as you know it gives your code an
advantage on your particular platform.
Besides taking the address, there's always also the possibility to cast the
const away, isn't there? This then may also change the behavior.

Gerhard
Feb 23 '08 #5
Gerhard Fiedler <ge*****@gmail.comwrote:
On 2008-02-23 13:49:33, Pavel wrote:
Darin Johnson wrote:
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 applying a template without thinking, as if it were like "const
BigStruct &". But I'm wondering if there's some school of thought out
there that encourages this and I'm missing something subtle.
It was already implicitly mentioned but before changing make sure the
functions do not use the address of the parameter; otherwise, the
behavior may change. Barring that, I am not aware of any drawbacks of
what you are doing -- as long as you know it gives your code an
advantage on your particular platform.

Besides taking the address, there's always also the possibility to cast the
const away, isn't there? This then may also change the behavior.
Taking an address of the parameter or casting away constness would both
be dangerous...

void foo( const char& c ) {
// casting away constness would be undefined behavior and
// taking (and presumably saving) the address would be pointless.
}

int main() {
foo( '5' );
}
Feb 24 '08 #6
On Feb 23, 4:06 am, Jeff Schwab <j...@schwabcenter.comwrote:
Yes, there is. I have yet to see a compelling argument either for or
against.
Ok, thanks for replies everyone. I had checked the
compiler for PowerPC and it does use extra indirections
when used this way instead of optimizing it away.
(2 indirections, since the caller also has to put stuff
that would normally be in registers on the PowerPC
onto the stack first)

None of the code takes the address of the paramters,
and the functions aren't templated. I normally just
leave it alone, unless I've got the files checked out
and am modifying them anyway.

I had seen other styles that seemed "odd" until
others explained the reasoning behind them, and
wondered if this might be one of them.

--
Darin Johnson
Feb 25 '08 #7

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

Similar topics

3
by: Alexander Farber | last post by:
Hi, does anyone have an idea, why do I get the following error (I have to use g++296 on RedHat Linux as compiler): In file included from r_dir.cpp:9: r_obey.h:262: declaration of `const...
4
by: barney | last post by:
Hello, I' m using .NET System.Xml.XmlDOcument. When I do the following: XmlDocument xml = new XmlDocument(); xml.Load("blah"); .... xml.Save("blub"); I've got the problem that the following...
2
by: s88 | last post by:
Hi all: I saw the code likes... 7 #include <stdio.h> 8 int main(void){ 9 const char *const green = "\033[0;40;32m"; 10 const char *const normal = "\033[0m"; 11 ...
5
by: martin | last post by:
Hi, I would be extremly grateful for some help on producing an xml fragemt. The fragment that I wish to produce should look like this <Addresses> <Address>&qout;Somebody's Name&quot;...
4
by: C. J. Clegg | last post by:
A month or so ago I read a discussion about putting const ints in header files, and how one shouldn't put things in header files that allocate memory, etc. because they will generate multiple...
7
by: xgngli | last post by:
Hi all! I've taken some time on learning the difference between "pointers to const variables" and "const pointer variables". The question is: in the following code, can we change the contents of...
26
by: =?gb2312?B?wNbA1rTzzOzKpg==?= | last post by:
i wrote: ----------------------------------------------------------------------- ---------------------------------------- unsigned char * p = reinterpret_cast<unsigned char *>("abcdg");...
20
by: liujiaping | last post by:
I'm confused about the program below: int main(int argc, char* argv) { char str1 = "abc"; char str2 = "abc"; const char str3 = "abc"; const char str4 = "abc"; const char* str5 = "abc";
4
by: Virtual_X | last post by:
some function make the data type of it's parameters as "const char*" why not use char or char* instead what would be different
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.