On Sun, May 2, 2010 at 3:59 AM, Christopher Zimmermann <madro...@zakweb.de> wrote: > On Sat, 1 May 2010 14:11:22 +0200 Marc Espie wrote: > >> On Sat, May 01, 2010 at 11:39:00AM +0200, Christopher Zimmermann wrote: >> > Hi, >> > >> > the following piece of code compiles fine using g++ 4.2.4, but >> > fails using g++ 3.3.5 in the base system: >> > >> > error: operands to ?: have different types >> > >> > It is part of ptlib, which is the base library for opal, which in >> > turn is needed for ekiga, which I'm trying to port. >> > >> > What is your suggestion? Can anyone think of a workaround for >> > this or should I just compile it using eg++ 4.2.4 ? >> > >> > >> > Christopher >> > >> > >> > #include<err.h> >> > >> > #define WarnIfNULL(x) ((x) ? (x) : (warn("blub"),(x))) >> > >> > class A >> > { >> > protected: >> > int a; >> > }; >> > >> > class B : A >> > { >> > public: >> > void blub() >> > { >> > WarnIfNULL(A::a); >> > } >> > }; >> >> Why do some C++ programmer still use macros where they're not needed ? >> bunch of idiots, let them stay with C. >> >> #include<err.h> >> >> template<typename T> >> inline T WarnIfNULL(T x) >> { >> if (!x) >> warn("blub"); >> return x; >> } >> >> class A >> { >> protected: >> int a; >> }; >> >> class B : A >> { >> public: >> void blub() >> { >> WarnIfNULL(A::a); >> } >> }; >> > > ok, thanks. That seems to be the solution, still I have to wrap it in a macro, because I need __LINE__, __FILE__, __CLASS__...
You example obviously isn't showing the exact usage you require. I imagine the macro is being used in assignments, not just in the blub() method as you display. Cheers, --patrick $ cat fuck.c++ #include <stdlib.h> #include <err.h> #include <iostream> #define WarnIfNULL(x) ((!(x) && ((void)warnx("blub"),1)) ? (x) : (x)) class A { protected: int a; }; class B : public A { public: void blub(void) { WarnIfNULL(A::a); } }; int main(int argc, char *argv[]) { int bs = 10; B cxxsucks; cxxsucks.blub(); bs = WarnIfNULL(100); ::std::cout << "bullshit: " << bs << ::std::endl; exit(0); } $ c++ fuck.c++ /usr/lib/libstdc++.so.49.0: warning: strcpy() is almost always misused, please use strlcpy() /usr/lib/libstdc++.so.49.0: warning: strcat() is almost always misused, please use strlcat() $ ./a.out a.out: blub bullshit: 100