473,657 Members | 2,450 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

const parameter

Hi,

Could anybody tell me wh the parameter "T val" is not marked const in
this Stroustrup code, considering that val is not modified and not non-
const methods called?

template<class C, class Tint count(const C&v, T val)
{
typename C::const_iterat or i = find(v.begin(), v.end(), val);
int n = 0;
while (i != v.end()) {
++n;
++i; // skip past the element we just found
i = find(i, v.end(), val);
}
return n;
}
Jun 27 '08 #1
7 2077
On Apr 26, 8:52 pm, W Marsh <wayne.ma...@gm ail.comwrote:
Hi,

Could anybody tell me wh the parameter "T val" is not marked const in
this Stroustrup code, considering that val is not modified and not non-
const methods called?

template<class C, class Tint count(const C&v, T val)
{
typename C::const_iterat or i = find(v.begin(), v.end(), val);
int n = 0;
while (i != v.end()) {
++n;
++i; // skip past the element we just found
i = find(i, v.end(), val);
}
return n;

}
Hello,

You would expect the value parameter to be of type 'const T&',
especially since this is the type used in the standard version of
'count'. You could query Stroustrup about it (address in FAQ
http://www.research.att.com/~bs/bs_faq.html).

Regards.
Jun 27 '08 #2
On Apr 26, 8:52 pm, W Marsh <wayne.ma...@gm ail.comwrote:
Could anybody tell me wh the parameter "T val" is not marked
const in this Stroustrup code, considering that val is not
modified and not non- const methods called?
template<class C, class Tint count(const C&v, T val)
{
typename C::const_iterat or i = find(v.begin(), v.end(), val);
int n = 0;
while (i != v.end()) {
++n;
++i; // skip past the element we just found
i = find(i, v.end(), val);
}
return n;
}
Because there's no real point in it? The const would be ignored
at the interface level (in the function declaration). One can
argue both ways in the function definition, but in practice,
very few if any programmers use const here.

The argument for the const is that you'll get an error if you
accidentally modify the variable. The argument against is: who
cares? It's your variable, and you should be able to do what
you want with it. There's also the argument that you don't want
the meaningless const in the declaration, and you want the
declaration and the definition to be coherent. (This argument
probably applies less to templates, since you often won't have a
separate declaration.)

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #3
On 26 Apr., 20:52, W Marsh <wayne.ma...@gm ail.comwrote:
Hi,

Could anybody tell me wh *the parameter "T val" is not marked const in
this Stroustrup code, considering that val is not modified and not non-
const methods called?

template<class C, class Tint count(const C&v, T val)
{
*typename C::const_iterat or i = find(v.begin(), v.end(), val);
*int n = 0;
*while (i != v.end()) {
* *++n;
* *++i; // skip past the element we just found
* *i = find(i, v.end(), val);
*}
*return n;

}
Your question only makes sense if you meant that val should be passed
as a const reference (As explained by James Kanze, top level const
does not make to much sense). If I understood you correctly, I believe
this is a "bug". The code would function perfectly with the signature
(const C&v, const T& val).

/Peter
Jun 27 '08 #4
On Apr 28, 1:27 am, James Kanze <james.ka...@gm ail.comwrote:
On Apr 26, 8:52 pm, W Marsh <wayne.ma...@gm ail.comwrote:
Could anybody tell me wh theparameter"T val" is not marked
constin this Stroustrup code, considering that val is not
modified and not non-constmethods called?
template<class C, class Tint count(constC&v, T val)
{
typename C::const_iterat or i = find(v.begin(), v.end(), val);
int n = 0;
while (i != v.end()) {
++n;
++i; // skip past the element we just found
i = find(i, v.end(), val);
}
return n;
}

Because there's no real point in it? Theconstwould be ignored
at the interface level (in the function declaration). One can
argue both ways in the function definition, but in practice,
very few if any programmers useconsthere.

The argument for theconstis that you'll get an error if you
accidentally modify the variable. The argument against is: who
cares? It's your variable, and you should be able to do what
you want with it. There's also the argument that you don't want
the meaninglesscons tin the declaration, and you want the
declaration and the definition to be coherent. (This argument
probably applies less to templates, since you often won't have a
separate declaration.)

--
James Kanze (GABI Software) email:james.ka. ..@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Wouldn't marking a pass-by-value parameter as const allow the compiler
to optimize its use, as it does with local const variables? E.G.:

void foo(const int input) {
const int var = bar();
...
}
Jun 27 '08 #5
KenM wrote:
On Apr 28, 1:27 am, James Kanze <james.ka...@gm ail.comwrote:
>On Apr 26, 8:52 pm, W Marsh <wayne.ma...@gm ail.comwrote:
>>Could anybody tell me wh theparameter"T val" is not marked
constin this Stroustrup code, considering that val is not
modified and not non-constmethods called?
template<clas s C, class Tint count(constC&v, T val)
{
typename C::const_iterat or i = find(v.begin(), v.end(), val);
int n = 0;
while (i != v.end()) {
++n;
++i; // skip past the element we just found
i = find(i, v.end(), val);
}
return n;
}

Because there's no real point in it? Theconstwould be ignored
at the interface level (in the function declaration). One can
argue both ways in the function definition, but in practice,
very few if any programmers useconsthere.

The argument for theconstis that you'll get an error if you
accidentally modify the variable. The argument against is: who
cares? It's your variable, and you should be able to do what
you want with it. There's also the argument that you don't want
the meaninglesscons tin the declaration, and you want the
declaration and the definition to be coherent. (This argument
probably applies less to templates, since you often won't have a
separate declaration.)

Wouldn't marking a pass-by-value parameter as const allow the
compiler to optimize its use, as it does with local const
variables? E.G.:

void foo(const int input) {
const int var = bar();
...
}
The compiler knows that anyway.

If you make the parameter const and try to modify it, the compiler
will tell you that you cannot. If you don't modify it, the compiler
can use that knowledge as well, whether you mark the value const or
not.
Bo Persson
Jun 27 '08 #6
On May 5, 1:26 pm, "Bo Persson" <b...@gmb.dkwro te:
KenM wrote:
On Apr 28, 1:27 am, James Kanze <james.ka...@gm ail.comwrote:
On Apr 26, 8:52 pm, W Marsh <wayne.ma...@gm ail.comwrote:
>Could anybody tell me wh theparameter"T val" is not marked
constin this Stroustrup code, considering that val is not
modified and not non-constmethods called?
template<cla ss C, class Tint count(constC&v, T val)
{
typename C::const_iterat or i = find(v.begin(), v.end(), val);
int n = 0;
while (i != v.end()) {
++n;
++i; // skip past the element we just found
i = find(i, v.end(), val);
}
return n;
}
Because there's no real point in it? Theconstwould be ignored
at the interface level (in the function declaration). One can
argue both ways in the function definition, but in practice,
very few if any programmers useconsthere.
The argument for theconstis that you'll get an error if you
accidentally modify the variable. The argument against is: who
cares? It's your variable, and you should be able to do what
you want with it. There's also the argument that you don't want
the meaninglesscons tin the declaration, and you want the
declaration and the definition to be coherent. (This argument
probably applies less to templates, since you often won't have a
separate declaration.)
Wouldn't marking a pass-by-value parameter asconstallow the
compiler to optimize its use, as it does with localconst
variables? E.G.:
void foo(constint input) {
constint var = bar();
...
}

The compiler knows that anyway.

If you make the parameterconsta nd try to modify it, the compiler
will tell you that you cannot. If you don't modify it, the compiler
can use that knowledge as well, whether you mark the valueconstor
not.

Bo Persson
I don't quite get what you said. Are you saying the compiler can see
that the non-const parameter is never changed and so optimize as
though it were const? If so, why can't it do so with a local variable
like 'var' in the example?

Myself, I've never used const on a pass-by-value parameter- it seems
weird to me, but a colleague has started doing so and I'm trying to
figure out if it makes as much sense as any other use of const.

Thanks,
Ken
Jun 27 '08 #7
On May 6, 6:15 pm, KenM <ken.mck...@gma il.comwrote:
On May 5, 1:26 pm, "Bo Persson" <b...@gmb.dkwro te:
KenM wrote:
On Apr 28, 1:27 am, James Kanze <james.ka...@gm ail.comwrote:
>On Apr 26, 8:52 pm, W Marsh <wayne.ma...@gm ail.comwrote:
>>Could anybody tell me wh theparameter"T val" is not marked
>>constin this Stroustrup code, considering that val is not
>>modified and not non-constmethods called?
>>template<clas s C, class Tint count(constC&v, T val)
>>{
>> typename C::const_iterat or i = find(v.begin(), v.end(), val);
>> int n = 0;
>> while (i != v.end()) {
>> ++n;
>> ++i; // skip past the element we just found
>> i = find(i, v.end(), val);
>> }
>> return n;
>>}
>Because there's no real point in it? Theconstwould be ignored
>at the interface level (in the function declaration). One can
>argue both ways in the function definition, but in practice,
>very few if any programmers useconsthere.
>The argument for theconstis that you'll get an error if you
>accidentally modify the variable. The argument against is: who
>cares? It's your variable, and you should be able to do what
>you want with it. There's also the argument that you don't want
>the meaninglesscons tin the declaration, and you want the
>declaration and the definition to be coherent. (This argument
>probably applies less to templates, since you often won't have a
>separate declaration.)
Wouldn't marking a pass-by-value parameter asconstallow the
compiler to optimize its use, as it does with localconst
variables? E.G.:
void foo(constint input) {
constint var = bar();
...
}
The compiler knows that anyway.
If you make the parameterconsta nd try to modify it, the
compiler will tell you that you cannot. If you don't modify
it, the compiler can use that knowledge as well, whether you
mark the valueconstor not.
I don't quite get what you said. Are you saying the compiler
can see that the non-const parameter is never changed and so
optimize as though it were const? If so, why can't it do so
with a local variable like 'var' in the example?
It can. As long as the scope of a variable is local, and you
never take its address or a reference to it, it's fairly easy
for the compiler to identify all places where it is modified,
and treat it as if it were const everywhere else.
Myself, I've never used const on a pass-by-value parameter- it
seems weird to me, but a colleague has started doing so and
I'm trying to figure out if it makes as much sense as any
other use of const.
I find very few programmers using const on variables with auto
lifetimes. There isn't that much payback from it. (An
exception might be integral variables initialized with an
integral constant expression, which if const can also be used in
integral constant expressions. In practice, I almost always
make these static as well, however, and so they don't fall under
the above rule.)

In the case of function parameters, there is at least one very
good argument against it: the const is an internal detail, of no
relevance to the client, so you don't want it in the exported
declaration (in the header file). And for consistency, you want
the exported declaration and the definition to have exactly the
same form, right down to the spelling. Thus, something like:

MyFile.hh:
extern void funct( int param ) ;

MyFile.cc
#include "MyFile.hh"

void
funct( int const arg )
{
}

might be perfectly legal, but it violates most style rules.
Both because of the added const, and because of the change in
names.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #8

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

Similar topics

3
1960
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
7
1442
by: Richard Cavell | last post by:
Hi, The point of using const on a parameter to a function should be to let your compiler know that the parameter shouldn't be modified during your program. This allows you to keep your code safe and bug-free. Now, it also occurs to me that a const something-or-other could be passed as a reference (since it's guaranteed not to change) , or that the address of the object could be passed rather than the whole thing in the case of a...
14
2449
by: Enrico `Trippo' Porreca | last post by:
Given: typedef struct Node Node; struct Node { void *obj; Node *next; }; typedef struct Stack Stack; struct Stack {
18
1601
by: hzmonte | last post by:
typedef int t_compare_func(const void *, const void *); struct node *tree_search(struct node *root, const void *keyy, t_compare_func *comp) { struct node *cur_item; int result; if (root == NULL) return NULL; cur_item = root; while (cur_item != NULL) {
16
3153
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...
3
2334
by: awheeler | last post by:
Is it possible to pass an object reference to a function and ensure that the function is unable to modify the state of the object? Thanks.
6
2097
by: subramanian | last post by:
Consider the following program: #include <stdio.h> void myfn(const int **a) { static int i, j, k; a = &i; a = &j;
2
1712
by: subramanian100in | last post by:
Consider the following program: #include <stdio.h> void myfn(const int **a) { static int i, j, k; a = &i; a = &j;
11
1733
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) {
0
8421
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
8325
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
8742
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
8518
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
7354
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
5643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2743
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
1734
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.