Theres a certain style of coding that uses const as much as possible.
Like
const int foo(const int a, const int b)
{
const int retval = pow(a, b);
return retval;
}
one argument to use code like this is that compilers can theoretically
optimize better.
Is that true? I thought that was only with pointers? 41 2564
Serve Laurijssen wrote:
const int foo(const int a, const int b)
{
const int retval = pow(a, b);
return retval;
}
I read an article and it prefers the following style.
const int foo(int a, int b){
cosnt aa = a;
cosnt bb = b;
int retval;
/*...*/
retval = pow(aa, bb);
return retval;
}
lovecreatesbeau ty wrote:
cosnt aa = a;
cosnt bb = b;
sorry, the code will be better with the changes,
cosnt int ca = a;
cosnt int cb = b;
"lovecreatesbea uty" <lo************ ***@gmail.comha scritto nel messaggio
news:11******** *************@s 13g2000cwa.goog legroups.com...
lovecreatesbeau ty wrote:
cosnt aa = a;
cosnt bb = b;
sorry, the code will be better with the changes,
cosnt int ca = a;
cosnt int cb = b;
better:
const int ca = a;
const int cb = b;
:-)
Giorgio Silvestri
"Giorgio Silvestri" <gi************ **@libero.itha scritto nel messaggio
news:Dk******** ************@tw ister1.libero.i t...
>
"lovecreatesbea uty" <lo************ ***@gmail.comha scritto nel messaggio
news:11******** *************@s 13g2000cwa.goog legroups.com...
lovecreatesbeau ty wrote:
cosnt aa = a;
cosnt bb = b;
sorry, the code will be better with the changes,
cosnt int ca = a;
cosnt int cb = b;
better:
const int ca = a;
const int cb = b;
Or with:
#define cosnt const
Giorgio Silvestri
Serve Laurijssen posted:
Theres a certain style of coding that uses const as much as possible.
I myself have that style -- but with one exception: Return types.
Anything returned from a function is going to be an R-value in anyway, so
there's no need to add redundant words.
Here would be a sample function of mine:
int Func(int const a, int const b)
{
return a + b;
}
--
Frederick Gotham
On Sun, 23 Jul 2006 16:22:41 +0200, "Serve Laurijssen" <se*@n.tk>
wrote in comp.lang.c:
Theres a certain style of coding that uses const as much as possible.
Like
const int foo(const int a, const int b)
{
const int retval = pow(a, b);
return retval;
}
one argument to use code like this is that compilers can theoretically
optimize better.
Is that true? I thought that was only with pointers?
A function argument, or a local automatic variable, cannot be changed
without the compiler's knowledge unless its address is passed to a
function. So even rudimentary data flow analysis would permit any
optimizations on such a variable with or without the const qualifier.
Some programmers like to use const on function arguments like this as
a reminder that they don't intend to change them. Perhaps down near
the end of the function, they expect them to have the original passed
value. Example:
int foo(const int a, const int b)
{
/* do stuff using current value of a and b */
/* finally... */
ret = pow(a, b);
return ret;
}
Now if they accidentally modify a or b before they get to the final
expression that uses them, the compiler will issue a diagnostic for a
constraint violation.
I rarely try to argue with someone who adds minimal extra typing to
catch errors in their code, but I don't care much for this idea
myself. If the function is large enough that you could modify a or b
and not be seen on a single display screen in the editor at the point
where the value is used later, the function is too large and effort
would better be spent splitting it up.
As for putting a const qualifier on a returned object, that's pretty
useless. You can return a non-const object from a function that
returns a const object, and you can assign a const object returned
from a function to a non-const object in the caller. I don't see how
it achieves anything at all.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
"Serve Laurijssen" <se*@n.tkwrites :
Theres a certain style of coding that uses const as much as possible.
Like
const int foo(const int a, const int b)
{
const int retval = pow(a, b);
return retval;
}
one argument to use code like this is that compilers can theoretically
optimize better.
Is that true? I thought that was only with pointers?
<OT>
One of my radical language design ideas is to make all declared
objects const by default. If you want to be able to modify an
object's value, you'd need to explicitly qualify it with, say, "var".
Thus:
int x = 10;
var int y = 20;
x ++; /* error */
y ++; /* ok */
I suspect that most declared objects actually don't change in value
once they're initialized. If my suspicion is correct, the result
would be safer code.
Of course, such a radical change would be inappropriate for C, and I'd
strongly oppose it if there were any chance of it being taken
seriously. But designers of new languages might want to consider it.
</OT>
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Keith Thompson wrote:
<OT>
One of my radical language design ideas is to make all declared
objects const by default. If you want to be able to modify an
object's value, you'd need to explicitly qualify it with, say, "var".
Thus:
int x = 10;
var int y = 20;
x ++; /* error */
y ++; /* ok */
I suspect that most declared objects actually don't change in value
once they're initialized. If my suspicion is correct, the result
would be safer code.
Of course, such a radical change would be inappropriate for C, and I'd
strongly oppose it if there were any chance of it being taken
seriously. But designers of new languages might want to consider it.
</OT>
Isn't the same result achieved by declaring x as a macro ?
For example
#define x 10
in C. Most languages will have a similar construct and if the
programmer intends for the value to remain constant he/she
will pick that construct instead of one which allows for the value
to be changed. So how is your proposal different ?
Spiros Bousbouras sp****@gmail.co m writes:
Keith Thompson wrote:
><OT>
One of my radical language design ideas is to make all declared objects const by default. If you want to be able to modify an object's value, you'd need to explicitly qualify it with, say, "var".
Thus:
int x = 10; var int y = 20; x ++; /* error */ y ++; /* ok */
I suspect that most declared objects actually don't change in value once they're initialized. If my suspicion is correct, the result would be safer code.
Of course, such a radical change would be inappropriate for C, and I'd strongly oppose it if there were any chance of it being taken seriously. But designers of new languages might want to consider it.
</OT>
Isn't the same result achieved by declaring x as a macro ?
For example
#define x 10
in C. Most languages will have a similar construct and if the
programmer intends for the value to remain constant he/she
will pick that construct instead of one which allows for the value
to be changed. So how is your proposal different ?
In my (not very serious) proposal, objects would be read-only by
default, not necessarily constant in the sense of being computable
during compilation.
For example:
printf("Continu e? ");
fflush(stdout);
int c = getchar();
if (c == 'y') {
...
}
In my hypothetical language, any attempt to modify c after
initializing it would be illegal, unless it's qualified with "var".
The problem this tries to address is that many objects are declared as
variables (without "const") even though they're never actually
modified.
(Again, I absolutely am not advocating making this change to C.)
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Michael Klatt |
last post by:
Is there any practical difference between a local variable in main()
declared 'const' and one declared 'static const'?
int main()
{
static const int i1(0);
const int i2(0);
return 0;
}
|
by: JKop |
last post by:
Back when I read my first C++ book, I was given the following scenario:
class Cheese
{
public:
int number_of_holes;
int colour;
|
by: Steven T. Hatton |
last post by:
Any opinions or comments on the following? I don't say it below, but I came
out on the side of using enumerations over static constants.
/* I'm trying to figure out the pros and cons of using static const
* int class members as opposed to using enumerations to accomplish a
* similar goal. The use of static constants seems more explicit and
* obvious to me. Unless I assign values to the enumerators, the
* compiler will do it for me....
|
by: Hans |
last post by:
Hello,
Why all C/C++ guys write:
const char* str = "Hello";
or
const char str = "Hello";
|
by: Mantorok Redgormor |
last post by:
Is const really constant?
And on an OT note: how can I post with a modified e-mail address so I
don't get so much spam?
| |
by: Vijay Kumar R. Zanvar |
last post by:
Hello,
I have few questions. They are:
1. Is "const char * const *p;" a valid construct?
2. How do I align a given structure, say, at 32-byte boundary?
3. Then, how do I assert that a given object is aligned, say,
at 32-byte boundary?
|
by: Laurijssen |
last post by:
What are the advantages of using const as often as possible in C?
Does it help to declare integer function arguments as const? How about
pointers and automatic const integers?
const int func(const int x, const float *f)
{
const int retval = x;
return retval;
}
|
by: James |
last post by:
Hello,
First off, I know this code will not compile and am not asking for
someone to solve it for me.
What I am asking is from the code below, how does one first define an
array as a constant, and second pass the values in and out of
functions?
Here is the code; Thanks in advance:
|
by: Christopher Key |
last post by:
Hello,
Can anyone suggest why gcc (-W -Wall) complains,
test.c:22: warning: passing arg 1 of `f' from incompatible pointer type
when compiling the following code? Change the declation of f to,
void f(int *const *const p, int *const *const q) {
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |