Including Templated C++ in Objective C
I am trying to include a C++ library with quite a few templates into an objective C application.
It seems to perpetually choke on a few inline statements inside a shared library:
template <开发者_如何转开发;class T>
inline T MIN(T a, T b) { return a > b ? b : a; }
template <class T>
inline T MAX(T a, T b) { return a > b ? a : b; }
yielding the output:
expected unqualified-id before '{' token
expected `)' before '{' token
I am compiling with the options.
g++ -x objective-c++ -Wall -O3 -I. -c demod_gui.m -o demod_gui
All the other templates seem to compile fine, any idea what could be wrong here? Thanks in advance for any help.
There are already macros MIN
and MAX
defined in Foundation/NSObjCRuntime.h
.
#if !defined(MIN)
#define MIN(A,B) ({ __typeof__(A) __a = (A); __typeof__(B) __b = (B); __a < __b ? __a : __b; })
#endif
#if !defined(MAX)
#define MAX(A,B) ({ __typeof__(A) __a = (A); __typeof__(B) __b = (B); __a < __b ? __b : __a; })
#endif
So your definition becomes
template <class T>
inline T ({ __typeof__(T a) __a = (T a); __typeof__(T b) __b = (T b); __a < __b ? __a : __b; }) { return a > b ? b : a; }
which is obviously invalid.
Why not use std::max
and std::min
?
精彩评论