Title
Assignment from int to std::string
Status
nad
Section
[basic.string]
Submitter
Andrzej Krzemieński

Created on 2014-03-13.00:00:00 last changed 107 months ago

Messages

Date: 2016-03-01.20:42:47

Proposed resolution:

This should be addressed by a paper addressed to LEWG.

Date: 2018-06-23.17:54:13

[ LEWG: 2016-03, Jacksonville ]

is_integral<T>::valueis_arithmetic<tmpl-arg>::value

This needs a paper; close the issue

We don't think the breakage is acceptable.

Guidance to author: Look for a way to encourage a warning; discomfort with calling that "deprecation".

Consider += and push_back.

Date: 2016-03-01.20:42:47

[ Lenexa 2015-06-06: Move to LEWG ]

RS: std::string x('0' + n); broken by this.

MC: This is an extension, move to LEWG.

Move to LEWG, consensus.

Previous resolution [SUPERSEDED]:

This wording is relative to N3936.

  1. To [basic.string], class template basic_string synopsis, add as indicated:

    basic_string& operator=(const basic_string& str);
    basic_string& operator=(basic_string&& str) noexcept;
    basic_string& operator=(const charT* s);
    basic_string& operator=(charT c);
    template <class IntT> basic_string& operator=(IntT i) = delete;
    basic_string& operator=(initializer_list<charT>);
    
  2. Add after [string.cons] p26 as indicated:

    basic_string& operator=(charT c);
    

    -26- Returns: *this = basic_string(1,c).

    template <class IntT> basic_string& operator=(IntT i) = delete;
    

    -?- Remarks: This signature shall not participate in overload resolution unless is_integral<T>::value is true.

Date: 2014-03-13.00:00:00

The following code works in C++:

int i = 300;
std::string threeHundred;
threeHundred = i;

"Works" == "Compiles and doesn't have an undefined behavior". But it may not be obvious and in fact misleading what it does. This assignment converts an int to char and then uses string's assignment from char. While the assignment from char can be considered a feature, being able to assign from an int looks like a safety gap. Someone may believe C++ works like "dynamically typed" languages and expect a lexical conversion to take place.

Ideally the assignment from char could be deprecated and later removed, but as a less intrusive alternative one could consider adding a SFINAEd deleted function template:

template <typename IntT> // enable if is_integral<IntT>::value
basic_string& operator=(IntT) = delete;
History
Date User Action Args
2016-03-01 20:42:47adminsetmessages: + msg7995
2016-03-01 20:42:47adminsetstatus: lewg -> nad
2015-05-22 19:00:31adminsetmessages: + msg7431
2015-05-22 19:00:31adminsetstatus: new -> lewg
2014-03-25 21:17:44adminsetmessages: + msg6919
2014-03-13 00:00:00admincreate