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

在C++中从函数typedef生成函数指针typedef?

  •  0
  • Jeff  · 技术社区  · 11 年前

    可以从匹配的函数typedef干净地生成函数指针typedef吗?

    // Can I define fx_pt in terms of fx_t without duplicating the signature?
    // Does decltype allow for any new sort of trick here?
    using fx_t  = char(int, char, std::vector<float> const &);
    using fxp_t = char(*)(int, char, std::vector<float> const &);
    
    // Using function typedef to enforce interface for declarations and definitions.
    fx_t foo, bar, baz;
    char foo(int) { /* ... */ }
    
    // Using fp typedef elsewhere as normal.
    fxp_t whatever{bar};
    whatever(99);
    

    我从未期望函数(指针)typedef语法对我来说是完全直观的,但我无法确定消除重复代码是否可行。

    这可能与在类/基元类型def和类/基指针类型def之间转换的更大问题有关,我记得听说它们在。。。

    1 回复  |  直到 11 年前
        1
  •  5
  •   Praetorian Luchian Grigore    11 年前

    只需添加一个 * 到函数别名

    using fx_t  = char(int, char, std::vector<float> const &);
    using fxp_t = fx_t*;
    

    Live demo