Boost logo

Boost :

From: Olaf van der Spek (olafvdspek_at_[hidden])
Date: 2006-03-14 17:33:49


On 3/14/06, Reece Dunn <msclrhd_at_[hidden]> wrote:
> Olaf van der Spek wrote:
> > > I'm not sure. The code snippet you sent seems fine, so maybe the problem
> > > is elsewhere. I would check includes and namespaces.
> >
> > I've now got:
> > #include <boost/algorithm/string.hpp>
> > #include <string>
> > #include <vector>
> >
> > std::string f()
> > {
> > return "";
> > }
> >
> > int main()
> > {
> > std::vector<std::string> v;
> > boost::split(v, f(), boost::is_any_of(","));
> > return 0;
> > }
> >
> > split_test.cpp: In function 'int main()':
> > split_test.cpp:13: error: invalid initialization of non-const
> > reference of type 'std::string&' from a temporary of type
> > 'std::string'
>
> This is because f() is returning a std::string and this is being passed to the RangeT & argument in the split function. Due to C++ rules, performing the T -> T & conversion is invalid. Try defining f() as:
>
> const std::string f()
> {
> return "";
> }

Thanks, that did the trick. I understand T -> T& isn't valid (although
I don't like that), but why doesn't it automatically do T -> const T&?


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk