小编yar*_*yar的帖子

make Clang-Format忽略换行符的注释

是否有可能告诉Clang-Format忽略换行操作的注释?我们的想法是遵循"代码格式正确,即使评论超出换行余量"的风格.代码不应分成多行,如果它不超过保证金,但注释会.

例如

//desired behaviour:
short code = shortCode + 
        longlonglongCode;
short code = shortCode; //long comment without a line break

//not desired behaviour:
short code =
    shortCode;  //long comment without a line break
Run Code Online (Sandbox Code Playgroud)

c++ clang autoformatting clang-format

11
推荐指数
1
解决办法
1261
查看次数

如何捕获I/O异常(确切的I/O,而不是std :: exception)

我从这里尝试了示例程序(使用mingw-w64).该计划崩溃了.所以我编辑了它:

#include <iostream>     // std::cerr
#include <fstream>      // std::ifstream

int main()
{
    std::ifstream file;
    file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
    try {
        file.open("not_existing.txt");
        while (!file.eof())
            file.get();
        file.close();
    }
    catch (std::ifstream::failure e) {
        std::cerr << "Exception opening/reading/closing file\n";
    }
    catch (const std::exception& e) {
        std::cerr << "should not reach this";
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

现在它运行,但打印should not reach this,而我期待它打印Exception opening/reading/closing file.

为什么我的期望错了?

编辑:因为这似乎是一个重点,这是我的编译器的确切版本:mingw-w64版本"x86_64-6.2.0-posix-sjlj-rt_v5-rev1",即GCC版本6.2

c++ io exception-handling exception

7
推荐指数
1
解决办法
202
查看次数

对特征块的自动引用未按预期运行

我想使用对特征矩阵块的自动引用:

#include <Eigen/Dense>
using namespace Eigen;
void foo(MatrixXf& a)
{
    auto& a_block = a.block(2, 3, 4, 5);
    a_block = MatrixXf::Random(4,5);
}    
Run Code Online (Sandbox Code Playgroud)

这不能用 GCC 编译,因为a.block(2, 3, 4, 5)被评估为临时的,但a.block(2, 3, 4, 5) = MatrixXf::Random(4,5);工作得很好。

从我的角度来看,这不是预期的行为。这个问题有优雅的解决方案吗?这应该被视为对 Eigen 的错误/功能请求吗?

编辑:

使用auto而不是auto&解决问题!

该问题已被标记为Reference atemporary in msvc的重复项,但与 MSVC 无关。我也明确表示,很明显是a.block(2, 3, 4, 5)评估成临时的。问题是这是否是 Eigen 的正确行为。

c++ eigen c++11 eigen3

1
推荐指数
1
解决办法
906
查看次数