4th bullet point in iso §12.1 p5 doesn't make sense to me


J

johnkalane

Maybe I'm missing something, but IMO the 4th bullet point in iso §12.1 p5is wrong:

"X is a union and all of its variant members are of const-qualified type (or array thereof),"

Take for example the following snippet which compiles in Coliru and Ideone (http://ideone.com/QW45u7), but it shouldn't as the defaulted default constructor for the union should be deleted according to the bullet point alluded above.

union T
{
const int y;
const char c;
const float z;
T(int j) : y(j) {}
T() = default;
};

int main()
{
T t;
}

I also don't understand why does the Standard disallow this code (see note in iso §9.5 p2)

struct S
{
int i;
S() : i(1) {}
};

union T
{
S s;
char c;
};

int main()
{
T t;
}

but allows this. What's the difference?

struct S
{
int i;
S() : i(1) {}
};

union T
{
S s;
char c;
T() {}
};

int main()
{
T t;
}
 
Ad

Advertisements

V

Victor Bazarov

Maybe I'm missing something, but IMO the 4th bullet point in iso
§12.1 p5 is wrong:

"X is a union and all of its variant members are of const-qualified
type (or array thereof),"

First off, it's the bullet point from the enumeration of the cases when
a "defaulted default constructor is defined as deleted". In other
words, if you don't declare the c-tor or define it, it's *deleted* by
the compiler (the compiler cannot create one for you), and one of the
situations is the union with all members const.
Take for example the following snippet which compiles in Coliru and
Ideone (http://ideone.com/QW45u7), but it shouldn't as the defaulted
default constructor for the union should be deleted according to the
bullet point alluded above.

The bullet above does not apply in this case because you *explicitly*
declare the default constructor. The bullet only applies to the
_implicitly defined_ default c-tor.
union T
{
const int y;
const char c;
const float z;
T(int j) : y(j) {}
T() = default;
};

int main()
{
T t;
}

I also don't understand why does the Standard disallow this code (see note in iso §9.5 p2)

struct S
{
int i;
S() : i(1) {}
};

union T
{
S s;
char c;
};

int main()
{
T t;
}

but allows this. What's the difference?

struct S
{
int i;
S() : i(1) {}
};

union T
{
S s;
char c;
T() {}
};

int main()
{
T t;
}

The difference is that the union T in the latter (allowed) case is NOT
*implicit*.

V
 
J

johnkalane

Thanks for your reply, but I have to disagree.

"The bullet above does not apply in this case because you *explicitly*
declare the default constructor. The bullet only applies to the
_implicitly defined_ default c-tor."

As far as I can understand the term "defaulted default constructor" refers exactly to the defaulted (= default) default constructor T() as is explained here (http://stackoverflow.com/a/7469608/1042389). In other words, the "T()" in the expression "T() = default;" refers to the implicitly defineddefault constructor.
 
J

johnkalane

First off, it's the bullet point from the enumeration of the cases when

a "defaulted default constructor is defined as deleted". In other

words, if you don't declare the c-tor or define it, it's *deleted* by

the compiler (the compiler cannot create one for you), and one of the

situations is the union with all members const.




Ideone (http://ideone.com/QW45u7), but it shouldn't as the defaulted

default constructor for the union should be deleted according to the

bullet point alluded above.



The bullet above does not apply in this case because you *explicitly*

declare the default constructor. The bullet only applies to the

_implicitly defined_ default c-tor.

Thanks for your reply, but I have to disagree. As far as I can understand the "T()" in the expression "T() = default;" refers to the defaulted (= default) default constructor T(), as explained here http://stackoverflow.com/a/7469608/1042389.
 
A

Alf P. Steinbach

As far as I can understand the "T()" in the expression "T() = default;" refers
to the defaulted (= default) default constructor T(), as explained here
http://stackoverflow.com/a/7469608/1042389.

Stack Overflow is IMO a very unreliable authority, a Herb Schildt-land.
On SO an answer is selected as correct by the generally least competent
to judge it, namely the OP who, by asking, has demonstrated a clear lack
of competence. And this is so for ALL selected answers on SO, including
my own answers, and answers to my own questions, of course.

Rather than rely on SO, just assume that the standard's own definition
in §8.4.2/4 is the relevant one for use of that term in the standard:

* "Explicitly-defaulted functions and implicitly-declared functions are
collectively called /defaulted functions/, and the implementation shall
provide implicit definitions for them (12.1 12.4, 12.8), which might
mean defining them as deleted"

About your original question I'm less sure, but it does look like either
compiler bugs or a defect in the standard, or perhaps both.

I therefore recommend posting this to the mailing list about standards
defects (it has about the same role now as the defunct comp.std.c++),
namely <url:
http://groups.google.com/a/isocpp.org/group/std-discussion/>, and/or to
the comp.std.c++ sister group comp.lang.c++.moderated, which forked from
this group in the 90's, and which unlike comp.std.c++ is still limping
along, not quite dead.


Cheers & hth.,

- Alf
 
Ad

Advertisements

W

Wake up Brazil

Stack Overflow is IMO a very unreliable authority, a Herb Schildt-land.

On SO an answer is selected as correct by the generally least competent

to judge it, namely the OP who, by asking, has demonstrated a clear lack

of competence. And this is so for ALL selected answers on SO, including

my own answers, and answers to my own questions, of course.



Rather than rely on SO, just assume that the standard's own definition

in �8.4.2/4 is the relevant one for use of that term in the standard:



* "Explicitly-defaulted functions and implicitly-declared functions are

collectively called /defaulted functions/, and the implementation shall

provide implicit definitions for them (12.1 12.4, 12.8), which might

mean defining them as deleted"



About your original question I'm less sure, but it does look like either

compiler bugs or a defect in the standard, or perhaps both.



I therefore recommend posting this to the mailing list about standards

defects (it has about the same role now as the defunct comp.std.c++),

namely <url:

http://groups.google.com/a/isocpp.org/group/std-discussion/>, and/or to

the comp.std.c++ sister group comp.lang.c++.moderated, which forked from

this group in the 90's, and which unlike comp.std.c++ is still limping

along, not quite dead.





Cheers & hth.,



- Alf

From iso §8.4.2 p4:

"A special member function is user-provided if it is user-declared and not explicitly defaulted or deleted on its first declaration."

This seems to confirm what I said in my response to Victor Bazarov

Tks
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top