6

I'll try to explain what I'm trying to do:

bool if_filter_applies() {return true;}

#defile LOGFILE if( if_filter_applies() ) LOG_STREAM_OBJECT

void globalFunc() { LOGFILE << "Some data \n"; }

class C {
    int a;
    bool if_filter_applies() {
       if ( a == 1)
          return true;
       else
          return false;
       }
 public:
   void regMem () {
      LOGFILE << "Some data\n";
   }
   static void staticMem() {
      LOGFILE << "Some data\n";
   }
 };

I'm trying to modify a LOGFILE definition so that it ONLY writes to a stream when used from within a class's member function based on the output of the if_filter_applies() member function.

If LOGFILE is used from outside the class or in a static member function, I want it to use the Global if_filter_applies() function (which always returns true).

The above code does not compile, because static void staticMem() ends up using the if_filter_applies() class member instead of the global.

I don't want to create a different definition similar to #define LOGFILE as a replacement specifically for static member functions, because there are hundreds of files in our code and I don't want to replace all occurrences manually.

So are there any changes I can make to #defile LOGFILE macro so that it calls ::if_filter_applies() instead in the context of a static member function?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
SidR
  • 2,964
  • 1
  • 18
  • 32
  • Have a look at [this](http://stackoverflow.com/questions/25863485/is-it-possible-to-bring-global-function-into-the-overload-resolution-with-member?lq=1) (yes I'm equally disappointed) – Tom Knapen Mar 30 '16 at 14:07
  • Why would you want to **#defile** your LOGFILE :( – Ilja Everilä Mar 31 '16 at 11:32
  • It is not really a log file for my use case, it is actually a circular buffer. I just made LOGFILE up to explain my problem. :) – SidR Mar 31 '16 at 11:47

1 Answers1

3

If you're using MS Visual Studio, you can use the __if_exists check against this. So something like:

__if_exists(this)
{
    // In a member function
}
__if_not_exists(this)
{
    // Not in a member function
}
Buddy
  • 10,874
  • 5
  • 41
  • 58
  • Thanks for the answer! Unfortunately, I'm not using MS Visual Studio. Are there other equivalents for "__if_exists"? – SidR Mar 30 '16 at 05:54
  • I see that clang has a [-fms-extensions](http://llvm.org/releases/3.0/docs/ClangReleaseNotes.html#windows) option... could give that a try.. – Buddy Mar 30 '16 at 06:03
  • Thanks. Is there anything for linux systems? – SidR Mar 30 '16 at 09:15