Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 974
Stephen Diehl Avatar asked Jan 24 '26 21:01

Stephen Diehl


1 Answers

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?

like image 132
kennytm Avatar answered Jan 27 '26 11:01

kennytm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!