【问题标题】:Eigen unaryExpr fails with MSVC but works with GCCEigen unaryExpr 在 MSVC 中失败,但在 GCC 中有效
【发布时间】:2020-12-11 11:22:05
【问题描述】:

unaryExpr 无法在 MSVC 中的自定义 lambda 下将 X 类型的矩阵转换为 Y 类型,但可以与 GCC 和 clang 一起使用。有没有人遇到过这个?有什么解决方法吗?在我给出的示例中,我使用了一个矩阵,但在我的应用程序中,我使用了一个稀疏矩阵,因此它不像转换底层数组那么容易。

https://godbolt.org/z/TnKf7e

#include <Eigen/Core>
#include <string>

struct Expression {
    std::string d_ = "doesn't matter";
};

int main() {
    Eigen::Matrix<Expression, 3, 3> in;
    Eigen::Matrix3d out = in.unaryExpr([](const Expression& x) -> double { return 3.0; });
}

【问题讨论】:

    标签: c++ eigen eigen3


    【解决方案1】:

    在 MSVC 中设置 CXX 标准是不够的。这是因为 Eigen 正在检查 Meta.h 中 __cplusplus 的值。

    要编译此代码,请添加以下编译选项:/Zc:__cplusplus。

    在 CMake 上:

    set_property(TARGET main PROPERTY CXX_STANDARD 17)
    add_compile_options(/Zc:__cplusplus)
    

    Eigen 中的相关代码:

    #ifndef EIGEN_HAS_STD_RESULT_OF
    #if EIGEN_MAX_CPP_VER>=11 && ((__has_feature(cxx_lambdas) || (defined(__cplusplus) && __cplusplus >= 201103L)))
    #define EIGEN_HAS_STD_RESULT_OF 1
    #else
    #define EIGEN_HAS_STD_RESULT_OF 0
    #endif
    #endif
    

    关于 __cplusplus 的 MSVC 文章:https://devblogs.microsoft.com/cppblog/msvc-now-correctly-reports-__cplusplus/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-22
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-27
      • 1970-01-01
      相关资源
      最近更新 更多