win-lin

M

Michael Sgier

Hello
there's no definition of min or max on Windows but on Linux i get the
error:
max' undeclared (first use this function). How should i declare "max"?
WATERX is declared:
#define WATERX 96

#define CLAMPX( x ) ( min( WATERX-1, max( 0, x ) ) )

then secondly how can i write the linux equivalent to:

memset( waterForce, 0, sizeof( float ) * nVertices );

THANKS and regards
Michael
 
M

makc.the.great

Michael said:
How should i declare "max"?

how about:
#define max(x,y) ( ((x)>(y)) ? (x) : (y) )

beware that this way one of x or y will be re-evaluated.
 
?

=?ISO-8859-1?Q?Stefan_N=E4we?=

how about:
#define max(x,y) ( ((x)>(y)) ? (x) : (y) )

beware that this way one of x or y will be re-evaluated.

What's wrong with:

template<typename T>
const T& max(const T& x, const T& y)
{
return x<y ? y : x;
}


??


Stefan
 
R

Rolf Magnus

Stefan said:
What's wrong with:

template<typename T>
const T& max(const T& x, const T& y)
{
return x<y ? y : x;
}

What's wrong with:

#include <algorithm>
using std::max;
 
P

persenaama

<windows.h> includes other platform specific header (the name escapes
me at the moment), which defines min and max as *macros* (plain evil),
so that's what wrong with that unless do:

#define NOMINMAX
#include <windows.h>

-then- include <algorithm> (before or after previous snip), use
std::min and std::max happily ever after.

The information after this point is from weak memory, because haven't
worked with that old compiler for a while...

If using Microsoft compiler which is older than version 13.x.y (.net
2003 aka. 7.1) there is no min and max in std namespace, I recall (hope
correctly!) that the Visual C++ 6 is too old to implement the c++98 ..
so best bet is to upgrade the compiler and take care with <windows.h>
:)

Yes, I know that is platform specific information not best discussed
here so I am ready for a public flocking session on the town square
(sounds kinky.. mmm...)

FYI, I am assuming he got the min and max in the initial post through
including <windows.h>, as he mentions that "windows has and linux
doesn't" min and max. I could be wrong so more flocking, please!
(aaaah!)
 
K

Kristo

persenaama said:
<windows.h> includes other platform specific header (the name escapes
me at the moment), which defines min and max as *macros* (plain evil),
so that's what wrong with that unless do:

What's wrong with what? Please quote context when you post.
#define NOMINMAX
#include <windows.h>

-then- include <algorithm> (before or after previous snip), use
std::min and std::max happily ever after.

Huh? Why bother with that? If min and max are defined as macros as
you say, then they're not in the std namespace. Including both
<windows.h> and <algorithm> should not cause a name collision.

Kristo
 
P

persenaama

What's wrong with what? Please quote context when you post.

The context is including <windows.h> and <algorithm>, the context was
established already. The <windows.h> includes <WinDef.h>, which has
these nice preprocessor macro declarations there:

#define max(a,b) (((a) > (b)) ? (a) : (b))
....
#define min(a,b) (((a) < (b)) ? (a) : (b))

I retract my statement that is plain evil.. actually it is pretty darn
clever: no prefixes of anykind, lowercase name for the macros,
possibility to evaluate the a and b *twice* and other goodies. I think
that's plain GENIOUS! Apologies for thinking it was bad programming
practise and thus labeling it plain evil. :)


/*
Huh? Why bother with that? If min and max are defined as macros as
you say, then they're not in the std namespace. Including both
<windows.h> and <algorithm> should not cause a name collision.
*/

You sound so certain of this this, you know what, I believe you. Sorry
for the inconvenience of bothering you with any of this nonsense.
 
D

David Harmon

On 21 Oct 2005 09:02:49 -0700 in comp.lang.c++, "Kristo"
Huh? Why bother with that? If min and max are defined as macros as
you say, then they're not in the std namespace. Including both
<windows.h> and <algorithm> should not cause a name collision.

Macros do not respect namespace. If min and max are defined as
macros then std::min will expand to
std::whatever the macro expansion is

Worse yet, the macro will likely expand all over the <algorithm>
header itself when you try to include it.
 
D

deane_gavin

David said:
On 21 Oct 2005 09:02:49 -0700 in comp.lang.c++, "Kristo"


Macros do not respect namespace. If min and max are defined as
macros then std::min will expand to
std::whatever the macro expansion is

Worse yet, the macro will likely expand all over the <algorithm>
header itself when you try to include it.

I thought the same thing, so I was very surprised when gcc 3.4.2 under
Dev-C++ compiled this and the output was 2 as expected

#define max garbage
#include <algorithm>
#include <iostream>

int main()
{
std::cout << std::max(1,2) << std::endl;

return 0;
}

I swapped the first two lines around to

#include <algorithm>
#define max garbage

and got the error I expected: 'garbage' is not a member of 'std'

Comeau online behaves the same - successfully compiling the first
version and rejecting the second.

Rooting around inside <algorithm> I found
#undef min
#undef max

Is just the implementation taking care of a common issue, or is there
some requirement to #undef any macros that might otherwise stomp on the
std namespace? Other names in <algorithm> didn't appear to be awarded
this level of protection (although I might have missed it in the maze
of headers within headers).

Gavin Deane
 
M

Michael Sgier

Hello again
now the code runs under win but on linux i get:
error: name lookup of `y' changed for new ISO `for' scoping
at the followoing line:

for ( int y = 0, index = 0; y < WATERY; y++ )

also i can't find any declaration of index nor y. So i tried to substitute
y by yy and index by ind both declared as int. Now it compiles but not
with the desired effect. How comes?

Secondly what does the register do:

for ( register int c = 0; c < 5; c++ )

and third memset, what exactly does this?

memset( waterForce, 0, sizeof( float ) * nVertices );

THANKS and regards
Michael
 
M

Michael Sgier

those messages:

warning: name lookup of `y' changed
warning: matches this `y' under ISO standard rules
warning: matches this `y' under old rules

Hmmmpfh...
 
B

Branimir Maksimovic

Michael Sgier said:
Hello again
now the code runs under win but on linux i get:
error: name lookup of `y' changed for new ISO `for' scoping
at the followoing line:

for ( int y = 0, index = 0; y < WATERY; y++ )

also i can't find any declaration of index nor y. So i tried to substitute
y by yy and index by ind both declared as int. Now it compiles but not
with the desired effect. How comes?

No one can help you here.
Secondly what does the register do:

for ( register int c = 0; c < 5; c++ )

register gives a hint to the compiler that variable should be stored in
register of CPU.
and third memset, what exactly does this?

memset( waterForce, 0, sizeof( float ) * nVertices );

for(size_t i=0;i<sizeof( float )*nVertices;++i)
((char*)(void*)waterForce) = 0;

Greetings, Bane.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top