C++ Macro to replace text with nothing

泪湿孤枕 提交于 2020-03-26 05:42:09

问题


Macros are often used for text substitution. In my case, I need to conditionally wipe out some keywords, so that compilation will be possible in compilers that don't have the specific feature.

Specifically I've been looking into cpp11 range where this snippet comes from

template <typename C>
struct has_size {
    template <typename T>
    static constexpr auto check(T*) -> // problem in VS2013
        typename std::is_integral<
            decltype(std::declval<T const>().size())>::type;
// .. some more stuff
};

I'm providing this as the example that spawned the question. In the above code I ended up doing

template <typename T>
static 
#if COMPILE_WITH_GCC_NEW_ENOUGH
    constexpr 
#endif
auto check(T*) -> 

because there were other parts where constexpr needed to be replaced with const in order to compile.

What I'm asking is a way to say eg

#define Constexpr ?????????

so that it would be replaced by constexpr in gcc compilations and textually nothing in VS compilations.


回答1:


Certainly, simply:

#define constexpr

will expand constexpr to nothing. You can wrap this macro definition inside an appropriate #if for your compiler that doesn't support this.

Sometimes people make the "empty" expansion more explicit:

#define constexpr /* nothing */


来源:https://stackoverflow.com/questions/23351825/c-macro-to-replace-text-with-nothing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!