GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 19-Mar-2010, 02:41
ahbi82 ahbi82 is offline
Member
 
Join Date: Jul 2006
Posts: 265
ahbi82 has a spectacular aura aboutahbi82 has a spectacular aura about

About min and max


I have a matrix library with a namespace which have some min max operations. I understand that windows.h have macros for min max. I really don't why the compiler issues me error when i call the library min max. By the way i did a simple console program to illustrate. (Using VS2005)

CPP / C++ / C Code:
#include <iostream>

// It only compile if i add this #undef
#ifdef min
#undef min
#endif

namespace M
{
    class A
    {
        public:
            A() {};
            virtual ~A() {};
    }; // class A

    A min(A& _lhs, A& _rhs) 
    {
        // do nothing
        return A();
    };

}; // namespace M

void main(void)
{
    M::A a1, a2;
    M::min(a1, a2);
}

I thought with namespace prefix M would resolve this conflict, but i was wrong. I wonder if there's any other solution?
  #2  
Old 19-Mar-2010, 08:00
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,496
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: About min and max


Quote:
Originally Posted by ahbi82
...
I thought with namespace prefix M would resolve this conflict
The traditional Microsoft <windows.h> uses #defined macros for min() and max() so the preprocessor does a substitution for all uses of these functions before the compiler gets to see it. That means that putting a function in its own namespace won't help. The preprocessor doesn't look inside namespaces to see what to do with #defined macros. Putting it another way: #defined macros don't know about namespaces.

I note that some compiler vendors have implemented min() and max() in such a way that C++ programs with <windows.h> and min() and max() have implemented these as templated functions, so your program might very well compile without the #undef thingie. Of course that wouldn't help at all for C programs that use <windows.h>. See Footnote.

Quote:
Originally Posted by ahbi82
...any other solution?

Sure. Don't use <windows.h> or any other non-standard C++ headers. If you are creating a new program and writing your own classes or other functions, don't use #defined macros in for functions in your header files. Use templated functions or inline functions or whatever...

If that's not an option, then you can change your function's name to something that doesn't conflict with anything in any of the header files that you do include. For example, call your function m_max() or some such thing.

By the way, for users of <windows.h>, you might discover that older versions of Microsoft C++ libraries also implement things like abs() with #defined macros, but later versions use templated functions. See Footnote (again).

Regards,

Dave


Footnote: This is one reason that, speaking for myself, I generally advise programmers not to use #defined macros for functions. Sometimes there may seem to be a reasonably good reason for new programs to do this, but new programmers don't always realize what problems can arise from gratuitous use of #defined macros. Bad habits formed early in one's development are hard to break.

And, in case people feel obliged to point out that actually there are some things that are reasonably done with #defined macros used as functions:

I know that. If people get to the point in their development as programmers where they want do use them, I won't argue. (But I might reiterate my misgivings, and might even give a counterexample.)

My advice is just an opinion. Free advice, freely given, and worth exactly what you want it to be worth.

Period. Full stop.
Last edited by davekw7x : 19-Mar-2010 at 08:39.
  #3  
Old 19-Mar-2010, 08:42
ahbi82 ahbi82 is offline
Member
 
Join Date: Jul 2006
Posts: 265
ahbi82 has a spectacular aura aboutahbi82 has a spectacular aura about

Re: About min and max


Actually i never include windows.h. Thats why, i'm even more puzzled.
I haven try with the STL's std::min std::max......... i wonder if it too conflicts with the windows.h min max.

Anyway many thanks, Dave (footnote guy)!!
  #4  
Old 19-Mar-2010, 09:12
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,496
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: About min and max


Quote:
Originally Posted by ahbi82
Actually i never include windows.h.
Oops. That's what I get for assuming. I have been through the <windows.h> C++ wars with OPC (Other People's Code) a few times over the years, and I just jumped to the wrong conclusion. Maybe some day I will learn not try to guess at answers to problems that weren't actually expressed. (Don't laugh. It could happen, even after all these years. See Footnote.)

Anyhow: When I comment out the #ifdef stuff, your little do-nothing programlet compiles OK with my versions of Microsoft Visual C++ compilers (Version 6.0, 2005 Express, 2008 Express), using the command line
Code:
cl donothing.cpp /EHsc

How are you compiling? If it's from the Visual Studio IDE, maybe some project settings include some stuff that creates the conflict. I mean, I don't see any explanation for the behavior that you described unless the compiler is, somehow, somewhere, encountering a #defined macro for max().

So, my other recommendation is still an option: If you don't like the ugliness of the #ifdef stuff, you can change the name of your function.

Regards,

Dave

Footnote:

"They say that nobody is perfect. Then they tell you
practice makes perfect. I wish they'd make up their minds."

--Wilt Chamberlain
Nobody roots for Goliath
Last edited by davekw7x : 19-Mar-2010 at 10:06.
  #5  
Old 19-Mar-2010, 11:27
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 932
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: About min and max


With the #defines commented out I get this:
Code:
g++ -Wall -W -pedantic do_nothing.cpp -o do_nothing do_nothing.cpp:17: warning: unused parameter ‘_lhs’ do_nothing.cpp:17: warning: unused parameter ‘_rhs’ do_nothing.cpp:21: error: extra ‘;’ do_nothing.cpp:23: error: extra ‘;’ do_nothing.cpp:25: error: ‘::main’ must return ‘int’
If I remove those two ;'s (and return int like a good boy) it compiles.
gcc version 4.1.1 20061011 (Linux Red Hat 4.1.1-30)
 
 

Recent GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:03.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.