gpt4 book ai didi

c++ - 我应该如何编写一个像 MPL 中那样工作的元函数?

转载 作者:太空宇宙 更新时间:2023-11-04 11:50:47 24 4
gpt4 key购买 nike

在尝试编写调用 MPL 代码的元函数时,我似乎遗漏了一些东西。以下代码在 inst2 上编译失败并出现以下错误,但在 inst1 上运行正常:

错误 C2903:“应用”:符号既不是类模板也不是函数模板

using namespace boost::mpl;

template <typename VECTOR>
struct first_element : mpl::at_c<VECTOR, 0> {};

int main()
{
typedef vector<
vector<int, int>,
vector<int, int>,
vector<int, int>> lotso;

typedef mpl::transform<lotso,
first_element<_1>>::type inst1;

typedef first_element<
at_c<lotso,0>>::type inst2;

return 0;
}

最佳答案

我想你忘记了一个 ::type在调用at_c 的背后在 inst2 的 typedef 中.回想一下你的 first_element期待 at_c 上的东西可以应用。原始at_c<lotso, 0> , 但是,尚未评估。通过添加 ::type 来评估元函数。 .

#include <boost/mpl/vector.hpp>
#include <boost/mpl/at.hpp>
#include <boost/mpl/transform.hpp>
#include <boost/mpl/placeholders.hpp>
#include <type_traits>

using namespace boost;
using namespace boost::mpl;

template <typename VECTOR>
struct first_element : mpl::at_c<VECTOR, 0> {};

int main()
{
typedef vector<
vector<int, int>,
vector<int, int>,
vector<int, int>> lotso;

typedef mpl::transform<lotso,
first_element<_1>>::type inst1;

typedef first_element<
at_c<lotso,0>::type >::type inst2;
^^^^^^ <--- you forgot a ::type here

static_assert(std::is_same<first_element<inst1>::type, inst2>::value, "bingo");

return 0;
}

Live Example .作为进一步检查,我验证了对 inst1 的进一步取消引用给出与 inst2 相同的类型.

关于c++ - 我应该如何编写一个像 MPL 中那样工作的元函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18368870/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com