Boost logo

Boost :

Subject: Re: [boost] Concatenating int value to string ..??
From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2013-11-02 05:41:50


On Saturday 02 November 2013 15:05:45 Rahul Mathur wrote:
>
> On Sat, Nov 2, 2013 at 2:39 PM, Andrey Semashev
>
> <andrey.semashev_at_[hidden]>wrote:
> > On Saturday 02 November 2013 14:14:18 Rahul Mathur wrote:
> > > All,
> > >
> > > I wish to have string (combination of string value and int value) which
> > > increments until 100. The code is as below -
> > >
> > > ---
> > > #include <iostream>
> > > #include <cstdlib>
> > > #include <string>
> > > #include <boost/lexical_cast.hpp>
> > >
> > > using namespace std;
> > >
> > > int main() {
> > >
> > > std::string TID = "Apple";
> > > int TagID = 01, Value = 000 /* 100 IDs*/;
> > > std::string TagName, TagInitial, TagUId;
> > > TagName = TID + boost::lexical_cast<std::string>(TagID); //
> >
> > Should
> >
> > > be as -> Apple01
> > >
> > > TagInitial = TagName + boost::lexical_cast<std::string>(Value);
> >
> > //
> >
> > > Should be as -> Apple01000
> > >
> > > int i = boost::lexical_cast<int>(TagInitial) + 1; // Should be
> > > as
> > >
> > > -> Apple01001
> > >
> > > TagUId = boost::lexical_cast<std::string>(i);
> > > std::cout << "Tag ID .." << TagUId << endl;
> > >
> > > }
> > >
> > > I get below error message -
> > >
> > > --
> > > terminate called after throwing an instance of
> >
> > 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_i
> > nj>
> > > ector<boost::bad_lexical_cast>
> > >
> > > >'
> > > >
> > > what(): bad lexical cast: source type value could not be interpreted
> >
> > as
> >
> > > target
> > > Aborted (core dumped)
> > > --
> > >
> > > Any clue?
> >
> > Apple01000 cannot be interpreted as an integer, that's why
> > boost::lexical_cast<int>(TagInitial) throws.
>
> I don't think so.
>
> It compiled successfully ( $ g++ test.cpp -o test -lboost_system
> -lboost_thread ) with GCC-Boost v-1.54 but while linking and execution it
> gives error as shared.
>
> I think if I can know - How to support lexical or what is the build command
> to support lexical analysis of Boost during Boost-v1.54 build process with
> toolset of gcc? I do see lexical_cast.hpp file in include file of
> Boost-v1.54 while building.

I don't understand. You get a run time exception. You do know that
lexical_cast operates at run time, don't you?

PS: Don't top post, please.


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