【问题标题】:How do I use boost::adaptors::transformed to produce a range from a templated class and a vector?如何使用 boost::adaptors::transformed 从模板类和向量生成范围?
【发布时间】:2014-10-15 18:17:05
【问题描述】:

我之前问过一个关于使用 lambda 实现类似功能的问题,但 wasn't able to get this working 所以我尝试使用仿函数来解决问题。无论如何,这可能更整洁,因为它不涉及构造std::function 对象并且更接近example case set out in the documentation

这是一个简单的设置来说明我的问题:

#include <boost/range/adaptor/transformed.hpp>
#include <vector>

// some structure
template <typename T>
struct MyStruct
{
    MyStruct(T t)
    {
    }
};

template <typename T>
struct Converter
{
    using return_type = MyStruct<T>;

    return_type operator()(const T& value)
    {
        return return_type(value);
    }
};

int main(int argc, const char* argv[])
{
    std::vector<int> vec {1, 2, 3};

    auto val = vec | boost::adaptors::transformed(Converter<int>());

    return 0;
}

当我尝试编译时,我收到以下错误消息:

/home/user/packages/boost/mpl/eval_if.hpp:38:31: error: no type named 输入“boost::mpl::eval_if<boost::is_same<boost::use_default, boost::use_default>, boost::result_of<const Converter<int>(int&)>, boost::mpl::identity<boost::use_default> >::f_ {aka struct boost::result_of<const Converter<int>(int&)>}

我不知道该怎么做。我无法在代码中发现任何错误。有什么想法吗?

【问题讨论】:

    标签: c++ boost


    【解决方案1】:

    错误告诉您boost::result_of&lt;const Converter&lt;int&gt;(int&amp;)&gt; 没有type 成员。换句话说,函数调用运算符在使用const Converter&lt;int&gt; 对象时不起作用。一旦知道问题所在,就很容易看出问题所在:

    return_type operator()(const T& value) const
    //                                     ^^^^^
    {
        return return_type(value);
    }
    

    【讨论】:

    • 这么简单的事情让我很伤心!谢谢。您如何知道此错误与 const 说明符有关?
    • @Arman 这个错误基本上是说如果你有const Converter&lt;int&gt; conv; int i; 然后conv(i) 不起作用(我知道,以一种非常复杂的方式......)。一旦你理解了它在说什么,那么在代码中找到问题就很容易了。
    • @arman 更具体地说,假设你在哪里 boost::result_of&lt;const Converter&lt;int&gt;(int&amp;)&gt; 失败了,那是你的问题。你接下来会做什么?查找文档,使用它,从通话中删除&amp;s,删除const——等等,然后它就可以工作了。问题是const。如何解决?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-16
    • 1970-01-01
    • 1970-01-01
    • 2021-03-21
    • 2016-11-25
    • 2014-08-28
    • 1970-01-01
    相关资源
    最近更新 更多