C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Re: get max of two constant ints as const-expr - without mac

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
White Wolf
Guest





PostPosted: Sun Sep 14, 2003 1:40 am    Post subject: Re: get max of two constant ints as const-expr - without mac Reply with quote



manfred wrote:
Quote:
I'm faced with a situation where i have a hard time replacing the
macro version of MAX with the templated version std::max. I've got a
bunch of small, constant integers (from a header file, controlled by
someone else), and i want to define an array; the size of the array
should be the equal to the maximum of the integers. while the macro
version compiles fine, the function or templated version fails to
compile. any way to get rid of macro MAX here? since everything is
known at compile time i would prefer a fixed-size array here, no
vector.

template <int I, int J> struct your_max {
enum e {
result = ((I<J)?J:I)
};
};

Quote:
const int i0 = 2;
const int i1 = 4;
// more ints...

// get the maximum, using a macro
#define MYMAX(a, b) ((a) > (b) ? (a) : (b))
const int maxA = MYMAX(i0, i1);

// get the maximum, using direct evaluation
const int maxB = i0 > i1 ? i0 : i1;

// get the maximum, using std::max
const int maxC = std::max(i0, i1);

// get the maximum, using something like std::max
const int& computeMax(const int& i0, const int& i1) { return i0 > i1 ?
i0 : i1; }
const int maxD = computeMax(i0, i1);

// now declare arrays

char arrA[your_max<i0,i1>::result]; // ok
char arrB[your_max<i0,i1>::result]; // ok
char arrC[your_max<i0,i1>::result]; // ok
char arrD[your_max<i0,i1>::result]; // ok

;-)

--
WW aka Attila



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Sebastian Kapfer
Guest





PostPosted: Sun Sep 14, 2003 11:31 am    Post subject: Re: get max of two constant ints as const-expr - without mac Reply with quote



On Sat, 13 Sep 2003 19:50:25 -0400, manfred wrote:

Quote:
I'm faced with a situation where i have a hard time replacing the
macro version of MAX with the templated version std::max. I've got a
bunch of small, constant integers (from a header file, controlled by
someone else), and i want to define an array; the size of the array
should be the equal to the maximum of the integers. while the macro
version compiles fine, the function or templated version fails to
compile. any way to get rid of macro MAX here? since everything is
known at compile time i would prefer a fixed-size array here, no
vector.

You can't use std::max, because it is implemented as a function. But look
at this:


#include <iostream>
#include <string>

template< int a, int b >
struct static_max
{
static const int value = (a > b) ? a : b;
};

const int const1 = 2, const2 = 5;

int main()
{
std::string b[ static_max< const1, const2 >::value ];
std::cout << sizeof(b) / sizeof(*b) << "n";
return 0;
}


No macros, but the code is at least as ugly :-)

--
Best Regards, | Hi! I'm a .signature virus. Copy me into
Sebastian | your ~/.signature to help me spread!


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.