First:
int k[] ={1,2,3,4,5};
Second:
struct slk
{
int k[] ={1,2,3,4,5};
};
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussio...@isocpp.org.
To post to this group, send email to std-dis...@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-discussion/.
But now after you've changed example, you are trying to initialize array of size two with five elements which is incorrect and I would never expect that.
Guys, are you seriously not seeing anything wrong with the fact that this compiles:
struct slk{
slk() {}
slk(int i) : k{ i, 7 } {} //note that I've change those brackets into braces otherwise the code is incorrect
int k[5] = { 1, 2, 3, 4, 5 };
};
and this doesn't:
struct slk{
slk() {}
slk(int i) : k{ i, 7 } {}
int k[] = { 1, 2, 3, 4, 5 };
};
even though the only difference between those two examples is that in one size of array is explicitly stated and in the second size of array to be expected to be deduced by compiler which need I remind you, compiler already can do it and does it in other scenarios.
On 2015–03–16, at 6:04 PM, atch...@gmail.com wrote:struct slk{
slk() {}
slk(int i) : k{ i, 7 } {}
int k[] = {1, 2, 3, 4, 5};
};
It's unnatural, counter-intuitive, illogical and hurtful.
Daniel, thank you for your input. Agree with what you've said and that is basically on the lines of my thinking. Could you perhaps think of any example in which size of that array would be difficult (for compiler) to deduce from the initializer list that is.
Size of k is deduced from the initializer.
Why unused initializer? Surely the array would be initialized from that initializer so that initializer is used?
In the example you've provided:
a) array size always == 2
b) t must be convertible to type of what initializer list's element type is.
Daniel, thank you for your input. Agree with what you've said and that is basically on the lines of my thinking. Could you perhaps think of any example in which size of that array would be difficult (for compiler) to deduce from the initializer list that is.
--
However, the standard doesn't explicitly disallow what Arthur wants, and it can be read to allow for an array of unknown bound as a non static data member, provided an initializer exists.
On Monday 16 March 2015 20:28:15 Ville Voutilainen wrote:
> > template<int> struct T;
> > template<> struct T<1> { template<int,int> char a(int); };
> > template<> struct T<2> { static int a; };
> > struct X {
> >
> > char a[] = { T<sizeof(X)>::a<0, 0>(0) };
> >
> > };
> >
> > What's the bound of X::a, 1 or 2? (Note that X is a complete type in this
> > context, because the initializer is handled as if it appeared in a
> > constructor, so sizeof(X) is valid.)
>
> Except X can't be complete before it knows its size, which it can't know
> before it uses the initializer for a, which it can't do because that
> requires knowing the
> size, and thus the merry-go-round spins until the compiler possibly
> says "oh, snap, stop".
template<int> struct T;
template<> struct T<1> { template<int,int> char a(int); };
template<> struct T<2> { static int a; };
struct X {
X();
char a[];;
};
X::X() : a{ T<sizeof(X)>::a<0, 0>(0) }
{}
Are you saying that X isn't complete in the X::X constructor declaration?
On Monday 16 March 2015 15:05:49 Richard Smith wrote:
> > template<int> struct T;
> > template<> struct T<1> { template<int,int> char a(int); };
> > template<> struct T<2> { static int a; };
> > struct X {
> >
> > X();
> > char a[];;
> >
> > };
> >
> > X::X() : a{ T<sizeof(X)>::a<0, 0>(0) }
> > {}
> >
> > Are you saying that X isn't complete in the X::X constructor declaration?
>
> That's ill-formed because X::a has an incomplete type. X would be complete
> in the X::X constructor, but the program was ill-formed before we got
> there. (And the example I gave is ill-formed for essentially the same
> reason.)
Ah, I see what you mean.
I tested it with Clang and it accepted. It triggered the extension for flexible
trailing types and assumes that the caller allocated enough space for the
elements used in the array.
Guys,
Taken from stackoverflow, but actually I came up against identical problem yesterday and was puzzled as to why my code doesn't compile:First:
int k[] ={1,2,3,4,5};
Second:
struct slk { int k[] ={1,2,3,4,5};
};
First compiles, second doesn't saying that there is too many initializers. Surely when looking at it soberly those two lines of code are identical and one being in a global namespace and second being in a namespace of struct shouldn't have any impact on compiler behavior. Why is it that in First example compiler can deduce the size and in second it either cannot or won't do it? AFAIC, First notion is more preferable from the point of view of being easier modifiable, avoiding silly mistakes when providing explicit size of array, heck, even Bjarne in either one of his books of papers suggest the first option, in the same vein as this example:
enum a { one = 1, two, three};
instead:
enum a {one = 1, two = 2, three = 3};
Why is second option not allowed?!? It goes against every logical instinct.
This disambiguity is resooved by referring to the nonterminal in italic and to the defined term in nonitalic. Yet the example I mentioned does refer to the grammar nonterminal since the use is in italic.