简体   繁体   中英

How do I use boost::adaptors::transformed to produce a range from a templated class and a vector?

I asked a question about using a lambda to achieve something similar earlier but wasn't able to get this working so I've tried approach the problem using a functor instead. This is probably neater anyway in the sense that it doesn't involve constructing std::function objects and is closer to the example case set out in the documentation .

Here is a simple setup that illustrates my problem:

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

When I try to compile this I am getting the following error message:

/home/user/packages/boost/mpl/eval_if.hpp:38:31: error: no type named 'type' in ' 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&)>} '

I'm not sure what to make of this. I can't spot any errors in the code. Any ideas?

The error tells you boost::result_of<const Converter<int>(int&)> doesn't have the type member. In other words, the function call operator doesn't work when a const Converter<int> object is used. Once you know the problem, it's easy to see what's wrong:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM