Search code examples
c++c++-climanaged

How do I use ConditionalAttribute in C++-CLI?


I have a C++-CLI ref class that exposes a profiling infrastructure implemented in C++.

In C++ I have the preprocessor directive PROFILING_ENABLED to determine whether the intrusive profiling functions are in the code or not.

When exposing these to managed code, I thought that using the managed ConditionalAttribute would be appropriate. But I struggle with the syntax.

Here is my attempt:

#ifdef PROFILING_ENABLED

// c++ macros are defined and active on the project level, I would like the 
// conditional attribute to be active as well.

#define MANAGED_PROFILING_ENABLED

// how do I define the managed conditional "MANAGED_PROFILING_ENABLED" in C++-CLI?



#endif

    public ref class Profiler
    {
    public:
        [ConditionalAttribute("MANAGED_PROFILING_ENABLED")] // this compile but always inactive
        static void PushRange(System::String ^ name, Color color);

        [ConditionalAttribute("MANAGED_PROFILING_ENABLED")]
        static void PopRange();
    };

I would like to achieve the following: If the native c++ preprocessor directive is active, the managed ConditionalAttribute should be active as well. If on the other hand the native c++ preprocessor directive is inactive, the managed ConditionalAttribute should be inactive.


Solution

  • The below standards document is pretty old. But assume that, may be still valid.

    https://www.ecma-international.org/publications/files/ECMA-ST/ECMA-372.pdf

    Go to section 29.4.3 (You can find below content about conditional attributes in c++/CLI).

    C++/CLI does not provide this ability; although attributes of this type are accepted, they have no affect on code generation or execution.