Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #68220
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: {} |
| Date | 2019-09-05 06:59 +1200 |
| Message-ID | <gtafsoFnafoU2@mid.individual.net> (permalink) |
| References | <7e1483e8-b52a-42a6-be3d-5e0e8ee9b437@googlegroups.com> |
On 05/09/2019 00:58, Daniel wrote:
> Consider
>
> #include <iostream>
> #include <initializer_list>
>
> struct arg_t
> {
> };
>
> constexpr arg_t arg = arg_t();
>
> class C
> {
> public:
> C(size_t n) {}
> };
>
> class A
> {
> public:
> explicit A(size_t n)
> {
> std::cout << "1\n";
> }
> A(std::initializer_list<C> item)
> {
> std::cout << "2\n";
> }
> A(arg_t, size_t n)
> {
> std::cout << "3\n";
> }
> A(arg_t, std::initializer_list<C> item)
> {
> std::cout << "4\n";
> }
> };
>
> int main()
> {
> A a{ { 2 } }; // (1)
> A b{ arg, { 2 } }; // (2)
> }
>
> Output (vs2019):
>
> 2
> 3
>
> I don't understand why one of these constructors is using the
> initializer_list, and the other is not. Explanation?
Does the compiler issue any warnings?
gcc gives your expected result (2,4), clang thinks you intended to call
A(arg_t, size_t n) and issues a warning
/tmp/x.cc:40:15: warning: braces around scalar initializer
[-Wbraced-scalar-init]
A b{ arg, { 2 } }; // (2)
^~~~~~
I'm not sure which one is correct in this case!
Cheers,
Ian.
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
{} Daniel <danielaparker@gmail.com> - 2019-09-04 05:58 -0700
Re: {} Ian Collins <ian-news@hotmail.com> - 2019-09-05 06:59 +1200
Re: {} Daniel <danielaparker@gmail.com> - 2019-09-04 12:48 -0700
Re: {} Manfred <noname@add.invalid> - 2019-09-05 14:12 +0200
Re: {} Öö Tiib <ootiib@hot.ee> - 2019-09-05 08:24 -0700
Re: {} Manfred <noname@add.invalid> - 2019-09-05 20:52 +0200
Re: {} Daniel <danielaparker@gmail.com> - 2019-09-05 13:23 -0700
Re: {} Öö Tiib <ootiib@hot.ee> - 2019-09-05 23:44 -0700
Re: {} Öö Tiib <ootiib@hot.ee> - 2019-09-06 02:06 -0700
Re: {} Daniel <danielaparker@gmail.com> - 2019-09-06 11:27 -0700
csiph-web