Why can't i construct a std::string from a single char in C++?

时间:2016-10-20 19:34:09

标签: c++ c++11 stdstring

make a std::string from a char. For example, won't compile,

#include <string>

int main()
{
    char c = 'a';
    std::string s(c);
    return 0;
}

in g++, i get this much error,

foo.cpp: In function 'int main()':
foo.cpp:6:20: error: no matching function for call to 'std::__cxx11::basic_strin
g<char>::basic_string(char&)'
     std::string s(c);
                    ^
In file included from i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/in
clude/c++/string:52:0,
                 from foo.cpp:1:
i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/bits/basic_s
tring.h:535:9: note: candidate: template<class _InputIterator> std::__cxx11::bas
ic_string<_CharT, _Traits, _Alloc>::basic_string(_InputIterator, _InputIterator,
 const _Alloc&)
         basic_string(_InputIterator __beg, _InputIterator __end,
         ^
i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/bits/basic_s
tring.h:535:9: note:   template argument deduction/substitution failed:
foo.cpp:6:20: note:   candidate expects 3 arguments, 1 provided
     std::string s(c);
                    ^
In file included from i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/in
clude/c++/string:52:0,
                 from foo.cpp:1:
i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/bits/basic_s
tring.h:465:7: note: candidate: std::__cxx11::basic_string<_CharT, _Traits, _All
oc>::basic_string(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type
, _CharT, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>;
_Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits, _Allo
c>::size_type = unsigned int]
       basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc())
       ^
i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/bits/basic_s
tring.h:465:7: note:   candidate expects 3 arguments, 1 provided
i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/bits/basic_s
tring.h:455:7: note: candidate: std::__cxx11::basic_string<_CharT, _Traits, _All
oc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char; _Traits = s
td::char_traits<char>; _Alloc = std::allocator<char>] <near match>
       basic_string(const _CharT* __s, const _Alloc& __a = _Alloc())
       ^
i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/bits/basic_s
tring.h:455:7: note:   conversion of argument 1 would be ill-formed:
foo.cpp:6:20: error: invalid conversion from 'char' to 'const char*' [-fpermissi
ve]
     std::string s(c);
                    ^
In file included from i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/in
clude/c++/string:52:0,
                 from foo.cpp:1:
i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/bits/basic_s
tring.h:445:7: note: candidate: std::__cxx11::basic_string<_CharT, _Traits, _All
oc>::basic_string(const _CharT*, std::__cxx11::basic_string<_CharT, _Traits, _Al
loc>::size_type, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<
char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits
, _Alloc>::size_type = unsigned int]
       basic_string(const _CharT* __s, size_type __n,
       ^
i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/bits/basic_s
tring.h:445:7: note:   candidate expects 3 arguments, 1 provided
i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/bits/basic_s
tring.h:427:7: note: candidate: std::__cxx11::basic_string<_CharT, _Traits, _All
oc>::basic_string(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, st
d::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type, std::__cxx11::basi
c_string<_CharT, _Traits, _Alloc>::size_type, const _Alloc&) [with _CharT = char
; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11:
:basic_string<_CharT, _Traits, _Alloc>::size_type = unsigned int]
       basic_string(const basic_string& __str, size_type __pos,
       ^
i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/bits/basic_s
tring.h:427:7: note:   candidate expects 4 arguments, 1 provided
i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/bits/basic_s
tring.h:411:7: note: candidate: std::__cxx11::basic_string<_CharT, _Traits, _All
oc>::basic_string(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, st
d::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type, std::__cxx11::basi
c_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; _Traits = std
::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_
CharT, _Traits, _Alloc>::size_type = unsigned int]
       basic_string(const basic_string& __str, size_type __pos,
       ^
i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/bits/basic_s
tring.h:411:7: note:   candidate expects 3 arguments, 1 provided
i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/bits/basic_s
tring.h:399:7: note: candidate: std::__cxx11::basic_string<_CharT, _Traits, _All
oc>::basic_string(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&) [w
ith _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<cha
r>]
       basic_string(const basic_string& __str)
       ^
i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/bits/basic_s
tring.h:399:7: note:   no known conversion for argument 1 from 'char' to 'const
std::__cxx11::basic_string<char>&'
i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/bits/basic_s
tring.h:391:7: note: candidate: std::__cxx11::basic_string<_CharT, _Traits, _All
oc>::basic_string(const _Alloc&) [with _CharT = char; _Traits = std::char_traits
<char>; _Alloc = std::allocator<char>]
       basic_string(const _Alloc& __a)
       ^
i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/bits/basic_s
tring.h:391:7: note:   no known conversion for argument 1 from 'char' to 'const
std::allocator<char>&'
i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/bits/basic_s
tring.h:380:7: note: candidate: std::__cxx11::basic_string<_CharT, _Traits, _All
oc>::basic_string() [with _CharT = char; _Traits = std::char_traits<char>; _Allo
c = std::allocator<char>]
       basic_string()
       ^
i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/bits/basic_s
tring.h:380:7: note:   candidate expects 0 arguments, 1 provided

is there a justification for this constructor missing.

this works but feels wrong,

#include <string>

int main()
{
    char c = 'a';
    std::string s;
    s += c;
    return 0;
}

any advice, thanks.

4 个答案:

答案 0 :(得分:5)

std::string has no constructor from a single char. You can see all available constructors here.

From the other side operator+= is overloaded for a single char.

As it is already mentioned, you can use the constructor:

basic_string( size_type count, 
              CharT ch, 
              const Allocator& alloc = Allocator() );

to create an std::string from a single char, like:

std::string s(1, 'a');

答案 1 :(得分:5)

A string is not a char, so I don't know why you would expect one to be convertible to the other. The former is a collection of the latter. Indeed, the justification is in the reverse direction - you would need justification for such a constructor to exist and there really isn't one. Having an implicit conversion from char to string would be very, very odd.

If you want a collection of a single element, there are constructors for that:

std::string s1(1, c); // (2) create a string composed of 1 repeated character c
std::string s2{c};    // (9) create a string based on this sequence of chars

答案 2 :(得分:5)

Here you go:

string const s{ 'A' };

The rumours that this is not supported, are exaggerations.

Technically this uses the initializer_list constructor.

答案 3 :(得分:4)

Yes, it is a bit odd that there is no single-character constructor, but there is a single-character operator+=.

You can use the constructor that accepts a character pointer and a copy count:

std::string s(&c, 1);

Or the constructor that accepts a single character and a repeat count:

std::string s(1, c);

Or, in C++11 and later, the constructor that accepts a std::initialization_list:

std::string s{c};
or
std::string s = {c};
相关问题