jeudi 23 août 2018

How to prohibit call of specific functions when using a certain macro

I have a macro defined more or less like this:

#define SOME_MACRO(Name) \
bool Function#Name()

This macro is often used with certain functions. Let's call one of them foo(). It is used in several files like this:

SOME_MACRO(Hello) {
   //do stuff here
   foo();
   //do more stuff here
}

A new macro FOO_MACRO that calls foo was created. From now on, when people call SOME_MACRO, I don't want them to call foo() directly. Instead, I want them to make use of FOO_MACRO, which is called before and outside SOME_MACRO.

The only way I could think of was to create a lambda function named foo inside the calls to SOME_MACRO. The newly defined foo would then output errors when called.

SOME_MACRO(Hello) {
   auto foo = [](){
      //error
   };
   foo(); //should generate error
   //do more stuff here
}

I don't actually know if this will compile but even if it works, I will have to do this one by one on every call of SOME_MACRO. It's exhausting and the code becomes repetitive.

One thing I did try was to change the SOME_MACRO definition into something like this:

#define SOME_MACRO(Name) \
lots of stuff here \
namespace {
   void foo () {}
}
bool Function#Name()

If I do this, calling foo will generate a compile error due to ambiguity of the call. This accomplishes my goal of not letting people call the function. But the error might be confusing to others. I want to be able to create an error that lets them know "don't call foo! Use FOO_MACRO instead!".

Is there any other way to achieve this? If possible, I really don't want to use the ambiguous call error.

I'm writing this from memory so there might be syntax errors in the sample codes.

Aucun commentaire:

Enregistrer un commentaire