public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/61961] New: New warning when initializer-list constructor chosen for uniform init that doesn't mean to use initializer_list
@ 2014-07-30 10:24 redi at gcc dot gnu.org
  2021-03-21 12:16 ` [Bug c++/61961] Add new warning when initializer-list constructor chosen for uniform init that doesn't intend " redi at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: redi at gcc dot gnu.org @ 2014-07-30 10:24 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61961

            Bug ID: 61961
           Summary: New warning when initializer-list constructor chosen
                    for uniform init that doesn't mean to use
                    initializer_list
           Product: gcc
           Version: 4.10.0
            Status: UNCONFIRMED
          Keywords: diagnostic
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redi at gcc dot gnu.org

#include <string>

int main()
{
  std::string s{1ul, 'a'};
}

It would be nice to get a warning here that the initializer-list constructor
was chosen rather than the string(size_type, value_type) constructor.

The semantics I suggest are to warn when:

* an initializer-list constructor is selected by overload resolution
* the elements of the braced-init-list are not all the same type
* another (non-initializer-list) constructor is viable

So no warnings for:

  std::string{'a', 'b'}               // elements are same type
  std::string{'0'+(x%10), '0'+(y%10)} // elements are same type
  std::string{'a', 'b', 'c', 'd', 0}  // no other viable constructor
  std::string{1ul, '0'+(x%10)}        // use char('0'+(x%10)) to get warning

but warnings for:

  std::string(1ul, 'a'}               // motivating case
  std::string{'a', 0}                 // use '\0' to suppress warning


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug c++/61961] Add new warning when initializer-list constructor chosen for uniform init that doesn't intend to use initializer_list
  2014-07-30 10:24 [Bug c++/61961] New: New warning when initializer-list constructor chosen for uniform init that doesn't mean to use initializer_list redi at gcc dot gnu.org
@ 2021-03-21 12:16 ` redi at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: redi at gcc dot gnu.org @ 2021-03-21 12:16 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61961

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Jonathan Wakely from comment #0)
> The semantics I suggest are to warn when:
> 
> * an initializer-list constructor is selected by overload resolution
> * the elements of the braced-init-list are not all the same type
> * another (non-initializer-list) constructor is viable

This would also be useful to warn for the CWG 2137 / PR 76262 / PR 85577 cases
where an initializer-list constructor is chosen for a braced-init-list with a
single element of the same type as the class being constructed. Users almost
always want A{a} to use a copy constructor, and are surprised when GCC chooses
an initializer-list constructor (which other compilers don't do).

So revised suggestion for the semantics:

* an initializer-list constructor is selected by overload resolution
* another (non-initializer-list) constructor is viable
* either the braced-init-list has more than one element and they are not all
the same type, or it has exactly one element and its type is the same as the
class being constructed.

I think this should be included in -Wall. If the initializer-list constructor
is really intended then the warning can be suppressed by explicitly converting
the initializers to the intended type, which more clearly expresses the
intended meaning of the code anyway.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-03-21 12:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-07-30 10:24 [Bug c++/61961] New: New warning when initializer-list constructor chosen for uniform init that doesn't mean to use initializer_list redi at gcc dot gnu.org
2021-03-21 12:16 ` [Bug c++/61961] Add new warning when initializer-list constructor chosen for uniform init that doesn't intend " redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).