簡體   English   中英

Eigen unaryExpr 使用 MSVC 失敗,但適用於 GCC

[英]Eigen unaryExpr fails with MSVC but works with GCC

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; });
}

在 MSVC 中設置 CXX 標准是不夠的。 這是因為 Eigen 正在檢查 Meta.h 中 __cplusplus 的值。

要編譯此代碼,請添加此編譯選項:/Zc:__cplusplus。

在 CMake 上:

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

本征中的相關代碼:

#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/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM