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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:45:47+00:00 2026-06-13T07:45:47+00:00

Code: #include <iostream> #include <string> #include <sstream> #include <algorithm> using std::cerr; using std::cout; using

  • 0

Code:

#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
using std::cerr;
using std::cout;
using std::stringstream;
using std::string;
using std::for_each;

void convert(const string& a_value)
{
    unsigned short i;
    if (stringstream(a_value) >> i)
        cout << a_value << " converted to " << i << ".\n";
    else
        cerr << a_value << " failed to convert.\n";
}

int main()
{
    string inputs[] = { "abc", "10", "999999999999999999999", "-10", "0" };
    for_each(inputs, inputs + (sizeof(inputs)/sizeof(inputs[0])), convert);
    return 0;
}

Output from Visual Studio Compiler (v7, v8, v9, v10):

abc failed to convert.
10 converted to 10.
999999999999999999999 failed to convert.
-10 converted to 65526.
0 converted to 0.

Output from g++ (v4.1.2, v4.3.4):

abc failed to convert.
10 converted to 10.
999999999999999999999 failed to convert.
-10 failed to convert.
0 converted to 0.

I expected the "-10" to fail to be converted to an unsigned short but it succeeds with the VC compilers. Is this a:

  • bug in the VC compilers ?
  • bug in the GNU compilers and I have an incorrect expectation ?
  • an implementation defined behaviour ?
  • 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-13T07:45:48+00:00Added an answer on June 13, 2026 at 7:45 am

    The answer depends on what version of C++ you are using. C++03 and
    earlier required the input to conform to what sscanf does (using here
    the "%hi" input specifier), and sscanf reads an integral value into
    a (signed) short, with no overflow detection; the results are then
    assigned (with implicit conversion) to your unsigned short. C++11
    requires the equivalent of calling strtoull, which doesn’t allow the
    - sign, and requires an error in case of overflow (which is undefined
    behavior in sscanf, and thus C++03).

    In practice, all reasonable implementations of C++03 did check for
    overlow, and the “undefined behavior” in such cases corresponded to that
    which is now required. On the other hand, they were required to
    accept the minus sign, which is now (logically) forbidden.

    EDIT (correction):
    On rereading the requirements of strtoull, I find that it does require accepting the minus sign. So as stupid as it seems, the standard does require input to an unsigned integral type to accept the minus sign. (Note too that the behavior of strtoull depends on the global C locale, which may accept additional posibilities.)

    EDIT (further clarification):
    As ectamur points out, this should be an error (in C++11), because (unsigned long long)( -10 ) will be too large to be represented in an unsigned short. On the other hand, it is still undefined behavior in pre-C++03 (which is perhaps what VC++ is conforming to—so whatever they do is “correct”).

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

Sidebar

Related Questions

I have the following code #include <iostream> #include<string> #include <sstream> using namespace std; struct
Here's my code so far: #include<iostream> #include<string> #include<fstream> using namespace std; int main() {
here is code for regular expression matching #include<iostream> #include<stdio.h> #include<string.h> using namespace std; int
The following code #include <iostream> using namespace std; int main() { const char* const
I am writing the code #include<sstream> #include<iostream> using namespace std; int main(){ strstream temp;
Consider the following code: #include <cstdlib> #include <iostream> #include <string> #include <vector> #include <algorithm>
Given such a code segment: #include <iostream> #include <iterator> #include <fstream> #include <string> using
I think following code should generate an error: #include <iostream> #include <string> static void
Consider the following code: #include <iostream> using namespace std; int main() { int x,
Given this code: #include <iostream> #include <vector> #include <fstream> #include <sstream> #include <boost/regex.hpp> using

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.