代码之家  ›  专栏  ›  技术社区  ›  101010

找出变量宏中的参数类型

  •  2
  • 101010  · 技术社区  · 7 年前

    MY_MACRO(...) )我这样称呼它:

    MY_MACRO(std::pair<int, int> const &p)
    

    现在, __VA_ARGS__ std::pair<int, int> const &p .

    有没有办法找出 __瓦乌阿格斯__

    想必,我会很感激 decltype(std::pair<int, int> const &p) std::pair<int, int> const& ,所以在我的变量宏的体内 decltype(__VA_ARGS__) 会屈服的 也。不幸的是,这不起作用。

    1 回复  |  直到 7 年前
        1
  •  5
  •   HolyBlackCat    7 年前

    你可以用 __VA_ARGS__ 作为lambda参数,然后将该lambda转换为函数指针并提取参数类型:

    template <typename T> struct func_param {};
    template <typename T> struct func_param<void(*)(T)> {using type = T;};
    
    #define FOO(...) \
        do \
        { \
            auto lambda = +[]([[maybe_unused]] __VA_ARGS__) {}; \
            using type = func_param<decltype(lambda)>::type; \
            /* Do something with `type`. */\
        } \
        while (0);
    
    推荐文章