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 8640825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:22:23+00:00 2026-06-12T11:22:23+00:00

Is there a standard library function to check that a string S is of

  • 0

Is there a standard library function to check that a string S is of type T and therefore can be converted to a variable of type T?

I know there is istringstream STL class that can, using operator>>, fill a variable of type T with a value converted from string. However, it will be filled with nonesense if the string content did not have the format of type T.

  • 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-12T11:22:24+00:00Added an answer on June 12, 2026 at 11:22 am

    The best you can do is try and fail, as @Cameron commented:

    #include <string>
    #include <sstream>
    #include <boost/optional.hpp>
    
    template <typename T>
    boost::optional<T> convert(std::string const & s)
    {
        T x;
        std::istringstream iss(s);
        if (iss >> x >> std::ws && iss.get() == EOF) { return x; }
        return boost::none;
    }
    

    Or, without boost:

    template <typename T>
    bool convert(std::string const & s, T & x)
    {
        std::istringstream iss(s);
        return iss >> x >> std::ws && iss.get() == EOF;
    }
    

    Usage:

    • First version:

      if (auto x = convert<int>(s))
      {
          std::cout << "Read value: " << x.get() << std::endl;
      }
      else
      {
          std::cout << "Invalid string\n";
      }
      
    • Second version:

      {
          int x;
          if (convert<int>(s, x))
          {
              std::cout << "Read value: " << x << std::endl;
          }
          else
          {
              std::cout << "Invalid string\n";
          }
      }
      

    Note that boost::lexical_cast is basically a cleverer version of this, which claims to be very efficient (potentially moreso than using iostreams unconditionally as we did here).

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

Sidebar

Related Questions

Is there a C++ Standard Template Library class that provides efficient string concatenation functionality,
Is there a cross-platform library function that would collapse a multiline string into a
Is there a function in the Delphi standard library to search string arrays for
Does anyone know why there is no snwprintf function in the C standard library?
Is there a standard Java library that handles common file operations such as moving/copying
Is there a standard way / C# library to convert a string into a
Is there a standard mechanism or known library that will convert .png images to
Is there python function in standard library like def cond(condition, true, false): if condition:
There are many endearing string functions in the C standard library, such as (in
I am wondering if there is a standard library function in Python which will

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.