Thread: Assigning int32_t to std::string, should this be allowed?

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    2

    Assigning int32_t to std::string, should this be allowed?

    Hi all, my compiler (gcc 4.4.3 on ubuntu 10.04 64bit server) is allowing me to assign an int32_t to a std::string object. The code is as follows:

    Code:
    #include <stdint.h>
    #include <iostream>
    int main()
    {
        int32_t i=1000000;
        std::string output;
        output=i;
        std::cout << output << std::endl;
        return 0;
    }
    which outputs:
    Code:
    @
    Obviously, this isn't something I should be doing, but instead something that I was allowed to mistakenly do. I would have expected a compiler error saying that no assignment operator existed for integer to std::string. Instead what I believe is happening is that the compiler is implicitly converting int32_t to char, and then assigning the char to the string.

    Is this a bug with the std::string class, with the compiler, or simply something I need to be more careful to avoid?

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    One of std::string's assignment operators takes one char parameter:

    string:perator= - C++ Reference

    int32_t is converted to char.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by frogger View Post
    Is this a bug with the std::string class, with the compiler, or simply something I need to be more careful to avoid?
    If you check, the length of output is 1, and 1000000%256 is 64, which is ascii for @. It isn't an implicit conversion, it's a straightforward assignment.

    I would assume that's what it's supposed to do by the standard. I would call it a feature, but like all features, if you don't know about it, it could be confusing.
    Last edited by MK27; 05-06-2011 at 03:24 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by MK27 View Post
    It isn't an implicit conversion, it's a straightforward assignment.
    There isn't implicit conversion from char to std::string, but there is one from int32_t to char.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's a narrowing conversion. Unfortunately, GCC doesn't warn about that (though Visual C++ does).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    2
    Thanks for the responses, I guess it makes sense if int32_t is being converted to char and char assignment to std::string is allowed. I wish gcc would at least warn about that conversion as Elysia reports Visual C++ does. Who knew MS would be more user friendly?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. assigning a string
    By steve1_rm in forum C Programming
    Replies: 5
    Last Post: 04-14-2008, 02:40 AM
  2. Replies: 8
    Last Post: 10-21-2007, 01:38 PM
  3. assigning a string to a variable
    By xp5 in forum C Programming
    Replies: 12
    Last Post: 08-30-2007, 01:13 AM
  4. Assigning string to values
    By Itanium in forum C++ Programming
    Replies: 3
    Last Post: 09-24-2003, 07:28 AM
  5. assigning string problem
    By sjfx in forum C++ Programming
    Replies: 2
    Last Post: 11-06-2002, 06:18 PM