小编use*_*419的帖子

有没有办法使用标准C++从GCC扩展模拟__typeof__?

这个问题的常见答案是标准C++ decltype具有与之相同的功能__typeof__.

哪个不是真的.虽然decltype只能接受表达式作为参数,__typeof__但也可以接受类型名称.

不管这看起来多么奇怪,我有一个真实的用例,我需要在没有扩展名的情况下使用此扩展程序将平台上的东西移植到平台上.表达式和类型都由宏传入,因此重写的版本必须同时接受这两者.

简化的示例伪代码:

// from header.h
#define MYMACRO(a)                                            \
                                                              \
    /* long code */                                           \
                                                              \
    using MyTypeA = decltype(some_processing_of #a)           \
    using MyTypeB = __typeof__(a);                            \
                                                              \
    /* long code */                                           \
                                                              \
    using MyType = std::conditional_t<                        \
        is_string_literal_test                                \
        , MyTypeA                                             \
        , MyTypeB >;                                          \
                                                              \
    /* long code */                                           \



// from use_case1.cpp
struct S {};
MYMACRO(S)



// from use_case2.cpp
MYMACRO("some text")



// example of __typeof__ in GCC that compiles …
Run Code Online (Sandbox Code Playgroud)

c++ gcc templates typeof decltype

5
推荐指数
0
解决办法
209
查看次数

标签 统计

c++ ×1

decltype ×1

gcc ×1

templates ×1

typeof ×1