No, there is no portable way to do this. Again, there are no portable ways to use #pragma. Because of this, many C / C ++ compilers define their own methods for creating pragma-like things, and they can often be embedded in macros, but for each compiler you need a different macro definition. If you want to go this route, you often do things like this:
#if defined(COMPILER_GCC) #define Weak_b #define Weak_e __attribute__((weak)) #elif defined(COMPILER_FOO) #define Weak_b __Is_Weak #define Weak_e #endif #define DEFINE_DELETE_OBJECT(type) \ Weak_b void delete_ ## type_(int handle) Weak_e; \ Weak_b void delete_ ## type(int handle) Weak_e;
In case it is not obvious that you want to define Weak_b and Weak_e as start and end bracketing constructors, because some compilers, such as GCC, add attributes as a complement to the type signature, and some, for example, MSC, add as a prefix ( or at least once, these have been the years since I used MSC). The bracketing contexts allow you to define what always works, even if you need to pass the entire type signature to the compiler construct.
Of course, if you try to port this to the compiler without the attributes you need, you won’t be able to do anything, but leave the macros nowhere and hope that your code will still work. In the case of a pure warning or optimization of pragmas, this is most likely. In other cases, not so much.
Oh, and I suspect that you really need to define Weak_b and Weak_e as macros that take parameters, but I didn’t want to read the documents to create a weak definition just for this example. I leave this as an exercise for the reader.
swestrup Jun 12 2018-10-12T00: 12
source share