undef min, define min macro ruins sstream

Alec Jacobson

January 09, 2013

weblog/

Depending on my header include order I got a large amount of errors from sstream:
In file included from /opt/local/include/gcc47/c++/istream:873:0,
                 from /opt/local/include/gcc47/c++/sstream:39,
                 from medit.h:69,
                 from ellipse.c:1:
/opt/local/include/gcc47/c++/bits/istream.tcc: In member function 'std::streamsize std::basic_istream<_CharT, _Traits>::readsome(std::basic_istream<_CharT, _Traits>::char_type*, std::streamsize)':
/opt/local/include/gcc47/c++/bits/istream.tcc:693:46: Error: error: expected unqualified-id before '(' token
In file included from /opt/local/include/gcc47/c++/sstream:581:0,
                 from medit.h:69,
                 from ellipse.c:1:
/opt/local/include/gcc47/c++/bits/sstream.tcc: In member function 'virtual std::basic_stringbuf<_CharT, _Traits, _Alloc>::int_type std::basic_stringbuf<_CharT, _Traits, _Alloc>::overflow(std::basic_stringbuf<_CharT, _Traits, _Alloc>::int_type)':
/opt/local/include/gcc47/c++/bits/sstream.tcc:112:39: Error: error: expected unqualified-id before '(' token
/opt/local/include/gcc47/c++/bits/sstream.tcc:114:35: Error: error: expected unqualified-id before '(' token
In file included from /opt/local/include/gcc47/c++/istream:873:0,
                 from /opt/local/include/gcc47/c++/sstream:39,
                 from medit.h:69,
                 from gisfil.c:1:
/opt/local/include/gcc47/c++/bits/istream.tcc: In member function 'std::streamsize std::basic_istream<_CharT, _Traits>::readsome(std::basic_istream<_CharT, _Traits>::char_type*, std::streamsize)':
/opt/local/include/gcc47/c++/bits/istream.tcc:693:46: Error: error: expected unqualified-id before '(' token
In file included from /opt/local/include/gcc47/c++/istream:873:0,
                 from /opt/local/include/gcc47/c++/sstream:39,
                 from medit.h:69,
                 from geometry.c:1:
/opt/local/include/gcc47/c++/bits/istream.tcc: In member function 'std::streamsize std::basic_istream<_CharT, _Traits>::readsome(std::basic_istream<_CharT, _Traits>::char_type*, std::streamsize)':
/opt/local/include/gcc47/c++/bits/istream.tcc:693:46: Error: error: expected unqualified-id before '(' token
make: *** [dlists.o] Error 1
In file included from /opt/local/include/gcc47/c++/sstream:581:0,
                 from medit.h:69,
                 from geometry.c:1:
/opt/local/include/gcc47/c++/bits/sstream.tcc: In member function 'virtual std::basic_stringbuf<_CharT, _Traits, _Alloc>::int_type std::basic_stringbuf<_CharT, _Traits, _Alloc>::overflow(std::basic_stringbuf<_CharT, _Traits, _Alloc>::int_type)':
/opt/local/include/gcc47/c++/bits/sstream.tcc:112:39: Error: error: expected unqualified-id before '(' token
/opt/local/include/gcc47/c++/bits/sstream.tcc:114:35: Error: error: expected unqualified-id before '(' token
In file included from /opt/local/include/gcc47/c++/sstream:581:0,
                 from medit.h:69,
                 from gisfil.c:1:
/opt/local/include/gcc47/c++/bits/sstream.tcc: In member function 'virtual std::basic_stringbuf<_CharT, _Traits, _Alloc>::int_type std::basic_stringbuf<_CharT, _Traits, _Alloc>::overflow(std::basic_stringbuf<_CharT, _Traits, _Alloc>::int_type)':
/opt/local/include/gcc47/c++/bits/sstream.tcc:112:39: Error: error: expected unqualified-id before '(' token
/opt/local/include/gcc47/c++/bits/sstream.tcc:114:35: Error: error: expected unqualified-id before '(' token
Turns out this was because the library I was linking with (medit) had the following lines:
#ifdef min                                                                                                
#undef min                                                                                                
#undef max                                                                                                
#endif
#define  min(a,b)       ( ((a) < (b)) ? (a) : (b) )                                                       
#define  max(a,b)       ( ((b) > (a)) ? (b) : (a) )      
Some how undefining min and max and then redefining them as these ruins sstream when included after.