【问题标题】:Is it possible to force a trailing return type to a new line with clang-format?是否可以使用 clang 格式将尾随返回类型强制转换为新行?
【发布时间】:2020-06-13 17:51:04
【问题描述】:

我正在寻找一种使尾随返回类型始终放在新行上的方法。我注意到 clang 格式会使用长声明来做到这一点,但如果它足够短则不会。有办法改变吗?

例如

auto foo() -> std::optional<std::string>
{ 
   // ...
}

变成

auto foo()
   -> std::optional<std::string>
{
   // ...
}

【问题讨论】:

    标签: c++ clang-format


    【解决方案1】:

    据我所知没有(可能是因为它是一个相对较新的功能)

    解决方法是在此处添加评论:

        auto foo() //
            -> std::optional<std::string>
        { 
           // ...
        }
    

    但是看到它没有作为标准实现,可能意味着它是一个非常不常见的符号,所以最好还是坚持默认

    【讨论】:

    • 尾随返回类型不是“相对较新的功能”;他们从 C++11 开始就存在了。即使在 2020 年撰写此答案时,它也已经存在了 9 年。该语法实际上早于clang-format 工具。
    猜你喜欢
    • 1970-01-01
    • 2012-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多