iso 12.1 p5中的第四个点对我来说没有意义 [英] 4th bullet point in iso 12.1 p5 doesn't make sense to me

查看:196
本文介绍了iso 12.1 p5中的第四个点对我来说没有意义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许我错过了一些东西,但IMO在第§12.1p5的第四个问题是错误的:


并且其所有变体成员都是const限定的
类型(或其数组),




从§9.1起,我们有:


在联合中,在任何时候最多一个非静态数据成员可以是活动的
,也就是说,最多一个非静态数据
成员可以随时存储在工会中。


编辑



此代码段不会在 Ideone 中编译

  union U 
{
const int i;
const char c;
float x;
U(int j,char a):i(j),c(a){}
};



Edit1:



以下代码在Coliru和 Ideone 。但根据12.1 p5第四个项目符号,它不应该作为默认构造函数应该删除。

  #include< iostream> ; 
union T
{
const int y;
const int x;
const float z;
T(int j):y(j){}
T()= default;
};

int main()
{
T t;
}

Edit2: b

我也不明白为什么标准不允许这个代码(见9.5 p2中的注释)

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

union T
{
S s;
char c;
};

int main()
{
T t;
}

有什么区别?

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

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

int main()
{
T t;
}


解决方案

5是完全有效的:它只是说如果所有的成员 const ,没有默认的构造函数可能生成,因为没有成员可以离开未初始化。



§9.1没有说明限制 const



这里只有一个成员是活跃的,这意味着只有一个可以以任意顺序写入和读取。



下面是一个示例,以及一个现场演示,演示该示例的 union 声明有效

  union T 
{
int x;
const int y;
const int z;
};

int main()
{
T a {4};
a.x = 5;
a.y = 6;
a.z = 7;
}

// $ g ++ - 4.8 -std = c ++ 11 -Wall -pedantic -pthread main.cpp&& ./a.out
// main.cpp:在函数'int main()'中:
// main.cpp:14:9:错误:赋值只读成员'T :: y'
// ay = 6;
// ^
// main.cpp:15:9:error:只读成员'T :: z'的赋值
// a.z = 7;
// ^



现场演示



T :: y






修改:您现在粘贴的代码和您所抱怨的错误消息与 const
的多个成员的初始化

code $


< >

这是你的问题。它说那么正确!尝试对联合会成员进行初始化没有任何意义,因为在任何给定时间只有一个可能是活动的:


[C ++ 11:12.6.2 / 8]: [..] 尝试初始化多个联合的非静态数据成员使程序形成错误。 [..]


但这与 const ness。


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),

simply because you can't have more than one const qualified member in a union.

From §9.1 we have:

In a union, at most one of the non-static data members can be active at any time, that is, the value of at most one of the non-static data members can be stored in a union at any time.

Edit:

This snippet doesn't compile in Ideone

union U
{
    const int i;
    const char c;
    float x;
    U(int j, char a) : i(j), c(a) {}
};

Edit1:

The following code compiles in Coliru and Ideone. But according to 12.1 p5 4th bullet point, it shouldn't as the default constructor should be deleted.

#include <iostream>
union T
{
    const int y;
    const int x;
    const float z;
    T(int j) : y(j) {}
    T() = default;
};

int main()
{
    T t;
}

Edit2:

I also don't understand why the Standard disallows this code (see note in 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;
}

解决方案

§12.1/5 is perfectly valid: it's just saying that no default constructor can possibly be generated if all the members are const, because then there is no member that can be left uninitialised.

§9.1 says nothing about limiting constness. It is a passage about limitations on accessing the union member's values, which is totally irrelevant.

Only one member being "active" here means that only one can be written to and read from in arbitrary sequence. As soon as you write to another member, you may only read from that member.

Here's an example, and a live demo showing that the example's union declaration is valid:

union T
{
    int x;
    const int y;
    const int z;
};

int main()
{
    T a{4};
    a.x = 5;
    a.y = 6;
    a.z = 7;
}

// $ g++-4.8 -std=c++11 -Wall -pedantic -pthread main.cpp && ./a.out
// main.cpp: In function 'int main()':
// main.cpp:14:9: error: assignment of read-only member 'T::y'
//      a.y = 6;
//          ^
// main.cpp:15:9: error: assignment of read-only member 'T::z'
//      a.z = 7;
//          ^

Live demo

Since compilation succeeds right up to the attempted assignment of T::y.


Edit: The code you've now pasted, and the error message you're complaining about, have nothing to do with the const.

initializations for multiple members of ‘U’

That is your problem. It says so right there! It makes no sense to try to initialise both members of a union, since only one may be "active" at any given time:

[C++11: 12.6.2/8]: [..] An attempt to initialize more than one non-static data member of a union renders the program ill-formed. [..]

But this has nothing at all to do with constness.

这篇关于iso 12.1 p5中的第四个点对我来说没有意义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆