This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/71590] G++ template function initialize with wrong type
- From: "msebor at gcc dot gnu.org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Tue, 21 Jun 2016 03:37:13 +0000
- Subject: [Bug c++/71590] G++ template function initialize with wrong type
- Auto-submitted: auto-generated
- References: <bug-71590-4 at http dot gcc dot gnu dot org/bugzilla/>
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71590
--- Comment #3 from Martin Sebor <msebor at gcc dot gnu.org> ---
There is a (subtle) difference between the initialization in "std::string s =
a;" and the assignment in "std::string s; s = a;" When valid, the
initialization invokes a constructor (possibly two), while the assignment
invokes the assignment operator. But since std::string doesn't have a
constructor that takes just a char (or int) argument the initialization is
invalid. (As mentioned, it does have an assignment operator that takes a
char.)
(For completeness' sake, in C++ 11, std::string has a constructor that takes a
std::initializer_list, and so with G++ a string object can be constructed like
so: "std::string s = { a };" But this is a G++ extension and not a valid C++
11 construct so G++ will give a warning: narrowing conversion of âaâ from âintâ
to âcharâ inside { }.