Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 三元运算符如何强制切线?_C++_Visual Studio_Visual Studio 2019_Clang Format - Fatal编程技术网

C++ 三元运算符如何强制切线?

C++ 三元运算符如何强制切线?,c++,visual-studio,visual-studio-2019,clang-format,C++,Visual Studio,Visual Studio 2019,Clang Format,如何强制截断(或保持手动截断)三值运算符行 // clang-formatted int foobar = bar ? a : b; // expected int foobar2 = bar ? a : b; 我从微软的风格开始。 这些是.clang format文件中适用的(我认为)设置,我是否缺少什么 AllowShortBlocksOnASingleLine: true AlignOperands: true Bre

如何强制截断(或保持手动截断)三值运算符行

// clang-formatted
int foobar = bar ? a : b;

// expected
int foobar2 = bar
                ? a
                : b;
我从微软的
风格开始。
这些是
.clang format
文件中适用的(我认为)设置,我是否缺少什么

AllowShortBlocksOnASingleLine: true
AlignOperands:   true
BreakBeforeTernaryOperators: true

部分关闭叮当格式怎么样?比如:

//关闭叮当格式
int foobar2=bar
? A.
:b;
//叮当作响
两种可能的解决方案(不同的回答中已经提到的
//clangformat off
)。但两者都不是很理想:

  • 设置样式选项
    ColumnLimit:0
    。这将导致clang format保留多行格式(如果它已经是多行格式)。但是如果三元运算符都在一行上,它就不会把它分成多行。这也可以通过使用预定义的
    WebKit
    样式来实现,该样式在内部设置
    ColumnLimit:0

    报告说:

    列限制为0表示没有列限制。在这种情况下,clang格式将尊重语句中输入的断行决定,除非它们与其他规则相矛盾

    当然,这样做的缺点是在所有代码中禁用通常的80个字符的列限制

  • 将注释放在
    之后的
    之前,如下所示:

     int foobar2 = bar //
                       ? a
                       : b;
    
    注释可防止clang格式将其合并回一行。但缺点是必须手动将注释添加到每个三元运算符中,这看起来有点难看


  • 虽然这从技术上回答了这个问题,但感觉有点像作弊;)毕竟,OP是在问如何让工具做一些特定的事情,并说根本不使用该工具,似乎没有那么有用。虽然从技术上讲这是可行的,但我宁愿不这样做。希望是自动格式化所有内容。我想我误解了问题中的“手动切碎”部分:)