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

The Archive Base Latest Questions

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

I want to make a parse function that excludes the type string or char

  • 0

I want to make a parse function that excludes the type string or char

template <typename T>
bool parse(T & value, const string & token){
    istringstream sin(token);
    T t;
    if( !(sin >>t) )  return false;
    char junk;
    if( sin >>junk )  return false;
    value = t;
    return true;
}

How can I do this?

  • 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-12T16:23:50+00:00Added an answer on June 12, 2026 at 4:23 pm

    Depending on what you mean by exclude the types string or char. If you want it not to link you can declare but not define specializations for the types:

    template <>
    void parse<std::string>( std::string & value, const std::string& token );
    

    The compiler will see the specialization and not generate the code. The linker will fail as the symbol is not defined in any translation unit.

    The second approach, a bit more complicated is not to fail at link time, but to make the compiler not accept the template for those types. This can be done with SFINAE, which is simpler in C++11, but if you need a C++03 solution you can google for it or add a comment:

    template <typename T, 
              typename = typename std::enable_if<!std::is_same<T,std::string>
                                              && !std::is_same<T,char>>::type >
    void parse( T & t, const std::string& token ) {
    // ...
    }
    

    (I have not run this through a compiler so the syntax might be a bit off, play with it) The compiler will see the template, and when it tries to perform the substitution of the type T it will fail due to the std::enable_if<...>::type not resolving to a type.

    In general, what you probably want is to provide different overloads that perform a specific version of the parse and take precedence:

    void parse( std::string& v, const std::string& token ) {
        v = token;
    }
    

    Note that this is not a template, but a regular function. A non-templated function will be a better match than a templated one when the arguments of the call are perfect matches.

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

Sidebar

Related Questions

I want to make a database restore function that will parse a dump made
I want to make one project that parse wiki pages and get needed information
I have a function that does xml parsing. I want to make the function
I want to make a log function in ASP and to do that I
I have link for example domain.com/de/controler/action?param=value and I want make actionlink to keep same
I'm want to make code that reads assembly instructions (x86 only) and recreates them
Suppose I want a function that takes a number and returns it as a
Hy! I want to make a autosuggest from my php file that returns a
I am attempting to abuse a reviver function with JSON.parse. I basically want to
I know that to parse a string to specific enum like: enum AnEnumType {

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.