 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Dhruv Guest
|
Posted: Wed Dec 24, 2003 10:56 pm Post subject: Which method would you prefer? |
|
|
Given that there are people with decadeds of expertise and experience on
this particular newsgroup, I'd like to know whether you would prefer
templates or macros to evaluate compile time expressions? eg:
//Assume you want to compute the greater of 2 ints, that are compile time
constant. Ok, the operatin to be performed will not be so simple, but just
for simplicity, assume that it is some complicated expression that can be
evaluated at compile time.
template <unsigned int lhs, unsigned int rhs>
struct MAX_UI {
enum { value = lhs > rhs ? lhs : rhs };
};
v/s:
#deifine MAX_MACRO(lhs,rhs) lhs > rhs ? lhs : rhs
Usage:
const int x = MAX_UI<23, 45>::value;
const int y = MAX_MACRO(23, 45);
Also, I'd like to know the particular use of unnamed enums. From what I
gather, they are somewhat similar to unnamed namespaces? Or have I gotten
it absolutely wrong?
Merry Christmas!
-Dhruv.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Joshua Lehrer Guest
|
Posted: Thu Dec 25, 2003 7:06 am Post subject: Re: Which method would you prefer? |
|
|
"Dhruv" <dhruvbird (AT) gmx (DOT) net> wrote
| Quote: | template <unsigned int lhs, unsigned int rhs
struct MAX_UI {
enum { value = lhs > rhs ? lhs : rhs };
};
v/s:
#deifine MAX_MACRO(lhs,rhs) lhs > rhs ? lhs : rhs
Usage:
const int x = MAX_UI<23, 45>::value;
const int y = MAX_MACRO(23, 45);
|
I would prefer the former, as you can't use it on run-time values.
The latter could be used, by accident, with run-time values, and we
all know how bad that can be.
On the other hand, the template, as you presented it, can only be used
for integral values. You'd have to add a bit of magic to let it work
for floating point values.
joshua lehrer
factset reseach systems
NYSE:FDS
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Thu Dec 25, 2003 2:33 pm Post subject: Re: Which method would you prefer? |
|
|
In message <pan.2003.12.24.18.06.44.881772 (AT) gmx (DOT) net>, Dhruv
<dhruvbird (AT) gmx (DOT) net> writes
| Quote: | Given that there are people with decadeds of expertise and experience on
this particular newsgroup, I'd like to know whether you would prefer
templates or macros to evaluate compile time expressions? eg:
//Assume you want to compute the greater of 2 ints, that are compile time
constant. Ok, the operatin to be performed will not be so simple, but just
for simplicity, assume that it is some complicated expression that can be
evaluated at compile time.
template <unsigned int lhs, unsigned int rhs
struct MAX_UI {
|
To start with I would not use a name without lowercase letters for
something which was not a pre-processor id.
| Quote: | enum { value = lhs > rhs ? lhs : rhs };
};
v/s:
#deifine MAX_MACRO(lhs,rhs) lhs > rhs ? lhs : rhs
|
As macros do not respect scope I would avoid them like the plague.
| Quote: |
Usage:
const int x = MAX_UI<23, 45>::value;
const int y = MAX_MACRO(23, 45);
Also, I'd like to know the particular use of unnamed enums.
|
I would not do that but it is to some extent a matter of style.
| Quote: | From what I
gather, they are somewhat similar to unnamed namespaces?
|
Not really. unnamed namespaces provide scopes enum (named or not) do not
provide scopes.
| Quote: | Or have I gotten
it absolutely wrong?
|
Misguided rather than absolutely wrong. There is no benefit to not
naming an enum type, and it is generally a good idea to avoid the
pre-processor if there is a sensible alternative.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
or http://www.robinton.demon.co.uk
Happy Xmas, Hanukkah, Yuletide, Winter/Summer Solstice to all.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dhruv Guest
|
Posted: Thu Dec 25, 2003 9:28 pm Post subject: Re: Which method would you prefer? |
|
|
On Thu, 25 Dec 2003 02:06:45 -0500, Joshua Lehrer wrote:
[...]
| Quote: | I would prefer the former, as you can't use it on run-time values.
The latter could be used, by accident, with run-time values, and we
all know how bad that can be.
|
But, wouldn't the compiler issue an error when that hapens?
I guess you are talking aboout something like this:
int x = 0;
cin>>x;
const int y = x;
Right?
Now, we cannot use y as compile time constant, or can we?
| Quote: | On the other hand, the template, as you presented it, can only be used
for integral values. You'd have to add a bit of magic to let it work
for floating point values.
|
How would I make it work for floats?
Regards,
-Dhruv.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dhruv Guest
|
Posted: Thu Dec 25, 2003 9:29 pm Post subject: Re: Which method would you prefer? |
|
|
On Thu, 25 Dec 2003 09:33:31 -0500, Francis Glassborow wrote:
[...]
| Quote: | I would not do that but it is to some extent a matter of style.
From what I
gather, they are somewhat similar to unnamed namespaces?
Not really. unnamed namespaces provide scopes enum (named or not) do not
provide scopes.
Or have I gotten
it absolutely wrong?
Misguided rather than absolutely wrong. There is no benefit to not
naming an enum type, and it is generally a good idea to avoid the
pre-processor if there is a sensible alternative.
|
I thought that they behaved similar to unnamed namespaces in the fact that
you can access the enum value without the :: operator as in:
enum Named { x = 1, y = 2 };
enum { Unnamed = 23 };
Usage would be:
Named: ;
Unnamed;
Right?
Regards,
-Dhruv.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dhruv Guest
|
Posted: Fri Dec 26, 2003 12:53 pm Post subject: Re: Which method would you prefer? |
|
|
On Thu, 25 Dec 2003 09:33:31 -0500, Francis Glassborow wrote:
[...]
| Quote: | Misguided rather than absolutely wrong. There is no benefit to not
naming an enum type, and it is generally a good idea to avoid the
pre-processor if there is a sensible alternative.
|
Oops, I should have written:
enum Named { x = 1, y = 2 };
enum { Unnamed = 23 };
Usage would be:
Named (0); Named (1); etc...
Or:
x, y, etc...
Unnamed;
Ok, so that clears my doubts... I was wrong in the earlier post, but now
I've understood where I was wrong!
Regards,
-Dhruv.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Carl Barron Guest
|
Posted: Fri Dec 26, 2003 1:07 pm Post subject: Re: Which method would you prefer? |
|
|
Dhruv <dhruvbird (AT) gmx (DOT) net> wrote:
| Quote: | Given that there are people with decadeds of expertise and experience on
this particular newsgroup, I'd like to know whether you would prefer
templates or macros to evaluate compile time expressions? eg:
//Assume you want to compute the greater of 2 ints, that are compile time
constant. Ok, the operatin to be performed will not be so simple, but just
for simplicity, assume that it is some complicated expression that can be
evaluated at compile time.
template <unsigned int lhs, unsigned int rhs
struct MAX_UI {
enum { value = lhs > rhs ? lhs : rhs };
};
v/s:
#deifine MAX_MACRO(lhs,rhs) lhs > rhs ? lhs : rhs
Usage:
const int x = MAX_UI<23, 45>::value;
const int y = MAX_MACRO(23, 45);
|
Macros are dangerous!!
MAX_MACRO above is particularly dangerous if used with expressions that
produce side effects , like MAX_MACRO(++x,++y); one of the two variables
will be incremented TWICE with MAX_MACRO !!
MACROS are visible in a file from the point of introduction to the end
of the file,including any files that include a file containing this
macro. This can result in redefinition errors changing what the macro
does in different portions of a file. a useful such redfinition is
includeing <cassert> more than once in a file to selecctively define
what the assert() macro does. This is documented and well known but
if done accidently with some other problem specific macro have fun
finding it give a core dump 'miles' from the macro's invocation...
it the macro is simple like #define PI 3.1415928
use const double PI = 3.1415928; for the same effect and no
problems with #define PI 0 siliently in a header and then
computing x = y/PI + x/(x+1)....
redefining a const double is an error and will be flagged!!!
if the macro defines an expression, placing that expression
in a template avoids multiple revaluation of expressions with
side_effects, and if it is inlined even avoids a real function call.
template <class T>
inline T my_max(const T &x,const T &y)
{
return x>y?x:y;
}
avoids a possible evaluate expression twice [in error] problem.
if you really want it for only compile time integral constants
then
template <int N,int M>
struct compile_time_max
{
static const int value = N>M ?N:M;
};
compile_time_max<3,4>::value is a const int = 4 at compile time.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Fri Dec 26, 2003 11:48 pm Post subject: Re: Which method would you prefer? |
|
|
In message <pan.2003.12.25.17.01.49.345785 (AT) gmx (DOT) net>, Dhruv
<dhruvbird (AT) gmx (DOT) net> writes
| Quote: |
I thought that they behaved similar to unnamed namespaces in the fact that
you can access the enum value without the :: operator as in:
enum Named { x = 1, y = 2 };
enum { Unnamed = 23 };
Usage would be:
Named: ;
|
Though one common compiler accepts that it is wrong because an enum does
not introduce a scope. Strictly Named: is illformed.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
or http://www.robinton.demon.co.uk
Happy Xmas, Hanukkah, Yuletide, Winter/Summer Solstice to all.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Joshua Lehrer Guest
|
Posted: Mon Dec 29, 2003 10:57 am Post subject: Re: Which method would you prefer? |
|
|
"Dhruv" <dhruvbird (AT) gmx (DOT) net> wrote
| Quote: | On Thu, 25 Dec 2003 02:06:45 -0500, Joshua Lehrer wrote:
[...]
I would prefer the former, as you can't use it on run-time values.
The latter could be used, by accident, with run-time values, and we
all know how bad that can be.
But, wouldn't the compiler issue an error when that hapens?
|
No. I meant that the macro could be used on run-time values, which
can be bad:
MAX_MACRO(func1(),func2());
if func1() and/or func2() is long running. The compiler would not
generate a warning on this.
-joshua lehrer
factset research systems
NYSE:FDS
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|