Combine boost::lexical_cast and std::transform
Asked Answered
R

2

6

I would like to write something like this, which cannot be compiled:

std::vector<A> as;
std::vector<B> bs( as.size() );
std::transform( as.beginn(), as.end(), bs.begin(), boost::lexical_cast<B> );

But this is not working, so I created a functor which is doing this for me:

template<typename Dest>
struct lexical_transform
{
    template<typename Src>
    Dest operator()( const Src& src ) const
    {
        return boost::lexical_cast<Dest>( src );
    }
};

Is there an easier way to do this?

Reverberation answered 13/2, 2010 at 20:0 Comment(0)
T
16

lexical_cast has two template arguments: target type and source type. Under normal usage, the second is deduced from the call.

However, here you want to take the address of the function, and you need to specify all the template arguments:

std::transform( as.begin(), as.end(), bs.begin(), boost::lexical_cast<B, A> );
Theressa answered 13/2, 2010 at 20:48 Comment(0)
A
1

If you do this kind of thing a lot you might want to consider the Boost.Convert library (not an accepted part of Boost yet). See this example from the manual:

std::transform(strings.begin(), strings.end(),
               std::back_inserter(integers),
               boost::convert<int>::from<string>());
Austine answered 13/2, 2010 at 20:53 Comment(2)
is there an advantage compared to the approach given by UncleBens?Reverberation
Not really for your example for in general it gives your more power. For instance you can use IO manipulators like std::hex or specify a default value that is used in case the conversion is not possibleAustine

© 2022 - 2024 — McMap. All rights reserved.