From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 17635 invoked by alias); 9 Nov 2012 18:29:17 -0000 Received: (qmail 17587 invoked by uid 48); 9 Nov 2012 18:28:58 -0000 From: "david at doublewise dot net" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/55254] New: Warn for implicit conversion from int to char Date: Fri, 09 Nov 2012 18:29:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: david at doublewise dot net X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Changed-Fields: Message-ID: X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated Content-Type: text/plain; charset="UTF-8" MIME-Version: 1.0 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org X-SW-Source: 2012-11/txt/msg00844.txt.bz2 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55254 Bug #: 55254 Summary: Warn for implicit conversion from int to char Classification: Unclassified Product: gcc Version: 4.7.2 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ AssignedTo: unassigned@gcc.gnu.org ReportedBy: david@doublewise.net I sometimes want to construct a string with n copies of a char c. However, I frequently get the order of elements in the constructor mixed up. Rather than saying std::string(80, '='), I accidentally call std::string('=', 80). To me, it seems like the underlying issue here is that gcc does not warn for implicit conversion from int to char. Whenever I assign a literal to a char, I always assign something wrapped in single quotes, never an integer literal. However, I would suggest that perhaps this warning should have two levels. The first level would only warn for char. The second level would warn for char, signed char, and unsigned char. The reason for this separation is that int8_t is a typedef for signed char and uint8_t is a typedef for unsigned char (on most platforms), and those are regularly used as "small integers" (I use them extensively in space-sensitive code). My experience is that when people use a signed / unsigned char explicitly, or one of the typedefs in cstdint / stdint.h, they are not used as actual characters, but bytes / small numbers, and in that case, assigning from an integer wouldn't be incorrect.