代码之家  ›  专栏  ›  技术社区  ›  Ashley Duncan

警告:函数使用“auto”类型说明符,但不带尾随返回类型

  •  10
  • Ashley Duncan  · 技术社区  · 7 年前

    MaxEventSize() '函数使用' auto

    int is)。

    template<typename T>
    constexpr T cexMax(T a, T b)
    {
        return (a < b) ? b : a;
    }
    
    constexpr auto MaxEventSize()
    {
        return cexMax(sizeof(int),
               cexMax(sizeof(int),
                        sizeof(int)));
    };
    
    2 回复  |  直到 7 年前
        1
  •  23
  •   bobbogo    5 年前

    这个 auto

    对于C++14,您的代码是可以的,但是对于C++11,如果您想使用 汽车

    auto funcName (args...) -> returnType
    

    你知道的 sizeof() std::size_t ,因此您的示例可以更正为

    constexpr auto MaxEventSize() -> std::size_t
    {
        return cexMax(sizeof(int),
               cexMax(sizeof(int),
                        sizeof(int)));
    };
    

    constexpr auto MaxEventSize() -> decltype( cexMax(sizeof(int),
                                                      cexMax(sizeof(int),
                                                             sizeof(int))) )
    {
        return cexMax(sizeof(int),
               cexMax(sizeof(int),
                        sizeof(int)));
    };
    
        2
  •  -1
  •   Zoe - Save the data dump 张群峰    5 年前

    为什么不直接使用模板T?
    更换管路

    constexpr auto MaxEventSize()  
    

    template<typename T>constexpr T MaxEventSize()