【问题标题】:C++ Macro to replace text with nothingC ++宏以无内容替换文本
【发布时间】:2014-04-28 21:54:40
【问题描述】:

宏通常用于文本替换。就我而言,我需要有条件地清除一些关键字,以便在没有特定功能的编译器中进行编译。

具体来说,我一直在研究 cpp11 range 这个 sn-p 来自哪里

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
};

我提供这个作为引发问题的示例。在上面的代码中我最终做了

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

因为还有其他部分需要将constexpr 替换为const 才能编译。

我要问的是一种表达方式

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

以便在 gcc 编译中将其替换为 constexpr,在 VS 编译中将其替换为 textually nothing

【问题讨论】:

  • 只需#define constexpr

标签: c++ macros


【解决方案1】:

当然,简单地说:

#define constexpr

constexpr 扩展为空。您可以将此宏定义包装在适当的 #if 中,以便您的编译器不支持此功能。

有时人们会使“空”扩展更加明确:

#define constexpr /* nothing */

【讨论】:

  • 我在包含守卫中看到了很多次,以至于我以某种方式推断出它只是定义一个用于检查#ifndef#ifdef 的符号。感谢您的快速响应
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-17
  • 1970-01-01
相关资源
最近更新 更多