Topic: const functions and metacode
Author: dhruvbird@gmx.net (Dhruv)
Date: Sat, 7 Jun 2003 21:36:57 +0000 (UTC) Raw View
Well, this is regarding the const keyword in the c++ programming
language, when used as a keyword to specify that a member function is
const, that is it does not modify it's class' member data. I was
wondering if there was a way to sepecify that the same function can be
specified to do stuff if the object is const or non-const, because it
is very tedious to write the same stuff again just for const objects
of that type. an example;
struct foo [ //the shift key does not work on this keyboard.
void bar [void];
void bar [void] const;
];
//this is what i have to do now. if the below were allowed, then rock
and roll.
struct foo [
void bar [void] const-non-const;
];
Now, getting to some posts that I've been reading on this or other
forums;
There is one very interesting post about functions being used as
constant expressions. And a certain Daveed has a very nice suggestion
to introduce metacode whuich would be evaluated at compile time
itself. Also, i had he chance to read a follow up to the post that
someone had already made a certain front-end which did something
similar to that. Now, the question i have is that how will the
metacode compiler handle requests for passing non-const parameters to
metacode functions. Two possibilities can exist [IMHO]. Don't quote me
on this but;
1. Introduce something like a keyword that specifies only-metacode,
which means that non-const stuff will not be allowed.
2. Make the compiler generate 2 copies of the function. Ok, there will
be only 1 copy technically, and the other[s] will be just a const
value which had been evaluated. Then, use the appropriate one wherever
needed. This might lead to problems across translation units though,
similar to the ones posed by inline functions [haven't gotten to terms
with that beast as yet, and still different compilers always seem to
produce different results].
This discussion was just constrained to functions, haven't thought of
other things as yet.
-Dhruv.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: eldiener@earthlink.net ("Edward Diener")
Date: Sat, 7 Jun 2003 22:54:22 +0000 (UTC) Raw View
Dhruv wrote:
> Well, this is regarding the const keyword in the c++ programming
> language, when used as a keyword to specify that a member function is
> const, that is it does not modify it's class' member data. I was
> wondering if there was a way to sepecify that the same function can be
> specified to do stuff if the object is const or non-const, because it
> is very tedious to write the same stuff again just for const objects
> of that type. an example;
>
> struct foo [ //the shift key does not work on this keyboard.
> void bar [void];
> void bar [void] const;
> ];
Aside from '[' and ']' not being correct above, there is a reason to write a
const and non-const version of a member function. If your member function
does not modify your class's data, simply write a const version and both
const and non-const objects will use that single version. If your member
function does modify your class's data, then if you want a const version of
your normally non-const member function, your const version has to be
different from your non-const version in order to have your const version
not modifying your class's data, and therefore you can not have the same
member function doing both things. Therefore your wish to have the same
member function being both const and non-const has no reason to exist.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: dhruvbird@gmx.net (Dhruv)
Date: Mon, 9 Jun 2003 01:37:21 +0000 (UTC) Raw View
eldiener@earthlink.net ("Edward Diener") wrote in message news:<j0uEa.39352$Io.3425282@newsread2.prod.itd.earthlink.net>...
> Dhruv wrote:
> > Well, this is regarding the const keyword in the c++ programming
> > language, when used as a keyword to specify that a member function is
> > const, that is it does not modify it's class' member data. I was
> > wondering if there was a way to sepecify that the same function can be
> > specified to do stuff if the object is const or non-const, because it
> > is very tedious to write the same stuff again just for const objects
> > of that type. an example;
> >
> > struct foo [ //the shift key does not work on this keyboard.
> > void bar [void];
> > void bar [void] const;
> > ];
>
> Aside from '[' and ']' not being correct above,
The shift key on this keyboard does not work.
there is a reason to write a
> const and non-const version of a member function. If your member function
> does not modify your class's data, simply write a const version and both
> const and non-const objects will use that single version. If your member
> function does modify your class's data, then if you want a const version of
> your normally non-const member function, your const version has to be
> different from your non-const version in order to have your const version
> not modifying your class's data, and therefore you can not have the same
> member function doing both things. Therefore your wish to have the same
> member function being both const and non-const has no reason to exist.
>
>
I guess this is one of those DUH questions that I ask once in a while.
The const version will work for both the const and non-const objects.
Thanks anyways.
-Dhruv.
> ---
> [ comp.std.c++ is moderated. To submit articles, try just posting with ]
> [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
> [ --- Please see the FAQ before posting. --- ]
> [ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]