Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8219237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:05:08+00:00 2026-06-07T13:05:08+00:00

Unfortunately, in my current project I can’t use boost, so I am trying to

  • 0

Unfortunately, in my current project I can’t use boost, so I am trying to imitate the behavior of boost::lexical_cast (minus most of the error checking boost does). I have the following functions, which work.

// Convert a string to a primitive, works
// (Shamelessly taken from another stack overflow post)
template <typename T>
T string_utility::lexical_cast(const string &in)
{
    stringstream out(in);

    T result;
    if ((out >> result).fail() || !(out >> std::ws).eof())
    {
        throw std::bad_cast();
    }

    return result;
}

// Convert a primitive to a string
// Works, not quite the syntax I want
template <typename T>
string string_utility::lexical_cast(const T in)
{
    stringstream out;
    out << in;

    string result;
    if ((out >> result).fail())
    {
        throw std::bad_cast();
    }

    return result;
}

I was hoping to be able to use the same syntax for both for consistency, but I can’t figure it out.

Converting a string to a primitive is fine.

int i = lexical_cast<int>("123");

The otherway, however, looks like this:

string foo = lexical_cast(123);

// What I want
// string foo = lexical_cast<string>(123);

Edit: Thanks ecatmur
I had to switch the template parameters around, but the following does exactly what I want.

template<typename Out, typename In> Out lexical_cast(In input)
{
    stringstream ss;
    ss << input;

    Out r;
    if ((ss >> r).fail() || !(ss >> std::ws).eof())
    {
        throw std::bad_cast();
    }

    return r;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T13:05:09+00:00Added an answer on June 7, 2026 at 1:05 pm

    The basic template code for lexical_cast is:

    template<typename In, typename Out> Out lexical_cast(In in) {
        stringstream ss;
        ss << in;
        if (ss.fail()) throw bad_cast();
        ss >> out;
        return out;
    }
    

    Add error checking and specialisations for (In == string), etc. as desired.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got to validate numerous dates with my current project. Unfortunately, these dates can
unfortunately I cannot resort to C# in my current project, so I'll have to
I want to use the ASSIMP library http://assimp.sourceforge.net in an iOS project. Unfortunately, I'm
In our current project we are providing a PDF download that can be customized
I am trying to use RubyMine to run the tests in an existing project.
I already tried getting the current URL of my UIWebView with: webview.request.URL . Unfortunately
Unfortunately, I have to use a C library with internal state in my Android
Unfortunately I haven't coded Java for about five years and I absolutely can not
For my current project, i'm using Swing and the Nimbus look and feel. The
I have some classes in my current project which have the wrong package declaration

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.