| View previous topic :: View next topic |
| Author |
Message |
Uenal Mutlu Guest
|
Posted: Thu Apr 21, 2005 11:25 am Post subject: redundant or not? |
|
|
Since in this class the member variable is marked as volatile
is then the volatile keyword in the function hdr redundant?
There is no other data member.
class X
{
public:
void add(int Ai) volatile
{
iVar += Ai;
}
private:
volatile int iVar;
};
|
|
| Back to top |
|
 |
Alberto Barbati Guest
|
Posted: Thu Apr 21, 2005 1:43 pm Post subject: Re: redundant or not? |
|
|
Uenal Mutlu wrote:
| Quote: | Since in this class the member variable is marked as volatile
is then the volatile keyword in the function hdr redundant?
There is no other data member.
class X
{
public:
void add(int Ai) volatile
{
iVar += Ai;
}
private:
volatile int iVar;
};
|
No. Consider the following code:
void foo(X volatile& x)
{
x.add(1);
}
If you remove "volatile" from the definition of add(), function foo()
becomes ill-formed.
Alberto
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Uenal Mutlu Guest
|
Posted: Thu Apr 21, 2005 2:57 pm Post subject: Re: redundant or not? |
|
|
"Alberto Barbati" wrote
| Quote: | Uenal Mutlu wrote:
Since in this class the member variable is marked as volatile
is then the volatile keyword in the function hdr redundant?
There is no other data member.
class X
{
public:
void add(int Ai) volatile
{
iVar += Ai;
}
private:
volatile int iVar;
};
No. Consider the following code:
void foo(X volatile& x)
{
x.add(1);
}
If you remove "volatile" from the definition of add(), function foo()
becomes ill-formed.
|
Hmm. I'm not that convinced.
Your foo() function simply extends the problem.
My argumentation is:
Since the member variable which the method add() modifies
is already defined as being volatile then the volatile keyword
following the method's parameter list is IMO redundant.
|
|
| Back to top |
|
 |
Alberto Barbati Guest
|
Posted: Fri Apr 22, 2005 12:50 pm Post subject: Re: redundant or not? |
|
|
Uenal Mutlu wrote:
| Quote: | "Alberto Barbati" wrote
Uenal Mutlu wrote:
Since in this class the member variable is marked as volatile
is then the volatile keyword in the function hdr redundant?
There is no other data member.
class X
{
public:
void add(int Ai) volatile
{
iVar += Ai;
}
private:
volatile int iVar;
};
No. Consider the following code:
void foo(X volatile& x)
{
x.add(1);
}
If you remove "volatile" from the definition of add(), function foo()
becomes ill-formed.
Hmm. I'm not that convinced.
Your foo() function simply extends the problem.
My argumentation is:
Since the member variable which the method add() modifies
is already defined as being volatile then the volatile keyword
following the method's parameter list is IMO redundant.
|
It depends on what you're trying to achieve. If you use the volatile
keyword on add() just as a cheap way to make all data members volatile,
then yes, it's redundant. However, you must realize that the semantic is
completely different because volatile on a member function participates
in overloading resolution, as my example shows, while volatile on data
members does not.
Alberto
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ali Çehreli Guest
|
Posted: Fri Apr 22, 2005 2:21 pm Post subject: Re: redundant or not? |
|
|
Uenal Mutlu wrote:
| Quote: | Since in this class the member variable is marked as volatile
is then the volatile keyword in the function hdr redundant?
|
The cv-qualifier in the function declaration/definition is separate from the
data members' costness or volatileness. They have two distinct roles. The
cv-qualifier in the function signature is used to determine which function
to call for a certain type of X object. The members' volatileness says how
the value of a member can change at run time.
| Quote: | There is no other data member.
|
It doesn't matter. Without the 'volatile' in the function signature, there
is no X::add to call for a volatile X object:
// Assume X::add is non-volatile
X volatile x;
x.add(); // compilation error
| Quote: |
class X
{
public:
void add(int Ai) volatile
|
The volatile above means that add can be called on volatile X objects. In
the absence of other add functions, that is the one that will be used for
non-volatile X objects as well.
Add another add, now the effect of the volatile above is obvious:
void add(int /* Ai */)
{
std::cout << "add non-volatilen";
}
The cv-qualifier in the function signature is used when binding the function
with the object.
| Quote: | {
iVar += Ai;
}
private:
volatile int iVar;
|
The above means that, apart from the entire object's volatileness, every X
object's iVar is volatile.
Here is a program with two X::add calls that are bound to separate add
functions:
#include
class X
{
public:
void add(int Ai) volatile
{
std::cout << "add volatilen";
iVar += Ai;
}
void add(int /* Ai */)
{
std::cout << "add non-volatilen";
}
private:
volatile int iVar;
};
int main()
{
X x;
X volatile xv;
x.add(1);
xv.add(2);
};
The program outputs
add non-volatile
add volatile
on my system. This is what I would expect :)
Ali
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|