这个模板标题中发生了什么?

发布于 2025-01-15 06:28:08 字数 1346 浏览 4 评论 0 原文

关于如何使用谷物序列化特征矩阵,有一个非常有用的答案: 使用 Cereal 库序列化 Eigen::Matrix

我复制并验证了此代码的工作原理,但我我很难理解标题中发生的事情:

template <class Archive, class _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols> inline
typename std::enable_if<traits::is_output_serializable<BinaryData<_Scalar>, Archive>::value, void>::type
save(Archive & ar, Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> const & m)
{
    int32_t rows = m.rows();
    int32_t cols = m.cols();
    ar(rows);
    ar(cols);
    ar(binary_data(m.data(), rows * cols * sizeof(_Scalar)));
}

第一行是采用谷物存档类型,然后是所有需要的特征模板参数。

我不是 100% 确定第二行在做什么,它似乎是在声明运行类型?但我无法理解这种类型的含义。

另外(如果可能,但不需要回答)

为什么这不起作用? (我没有检查过,它不会编译):



template <class Archive>
void serialize( Archive& archive, Eigen::Vector2f& vec )
{
    archive(
        CEREAL_NVP((float&)vec[0]),
        CEREAL_NVP((float&)vec[1])
    );
}

template <class Archive>
void serialize( Archive& archive, Eigen::Vector3f& vec )
{
    archive(
        CEREAL_NVP((float&)vec[0]),
        CEREAL_NVP((float&)vec[1]),
        CEREAL_NVP((float&)vec[2])
    );
}

There is an amazingly useful answer on how to serialize eigen matrices using cereal:
Serializing Eigen::Matrix using Cereal library

I copied and verified this code works, but I am having a hard time understanding what is going on in the header:

template <class Archive, class _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols> inline
typename std::enable_if<traits::is_output_serializable<BinaryData<_Scalar>, Archive>::value, void>::type
save(Archive & ar, Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> const & m)
{
    int32_t rows = m.rows();
    int32_t cols = m.cols();
    ar(rows);
    ar(cols);
    ar(binary_data(m.data(), rows * cols * sizeof(_Scalar)));
}

The first line is taking a cereal archive type and then all the needed eigen template parameters.

I am not 100% sure what the second line is doing, it seems to be declaring the run type? But I can't follow what the type is mean to be.

Additionally (if possible, but not required for an answer)

Why does this not work? (it doesn't I checked, it won't compile):



template <class Archive>
void serialize( Archive& archive, Eigen::Vector2f& vec )
{
    archive(
        CEREAL_NVP((float&)vec[0]),
        CEREAL_NVP((float&)vec[1])
    );
}

template <class Archive>
void serialize( Archive& archive, Eigen::Vector3f& vec )
{
    archive(
        CEREAL_NVP((float&)vec[0]),
        CEREAL_NVP((float&)vec[1]),
        CEREAL_NVP((float&)vec[2])
    );
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

朱染 2025-01-22 06:28:09

该模板使用 SFINAE(替换失败不是错误)来限制该函数采用的模板参数。

基本上,如果编译器认为模板函数的函数签名是错误的,它只会忽略该函数,而不是产生编译器错误。

typename std::enable_if<traits::is_output_serializable<BinaryData<_Scalar>, Archive>::value, void>::type

如果 traits::is_output_serialized, Archive>::value 计算结果为 true 并且会导致错误,则实际上是 void签名,如果其计算结果为 false

The template uses SFINAE (Substitution Failure Is Not An Error) to limit the template parameters this function takes.

Basically if the compiler considers the function signature of the template function to be erroneous, it simply ignores this function instead of producing a compiler error.

typename std::enable_if<traits::is_output_serializable<BinaryData<_Scalar>, Archive>::value, void>::type

is effectively void, if traits::is_output_serializable<BinaryData<_Scalar>, Archive>::value evaluates to true and it results in an erroneous signature, if it evaluates to false.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文