Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bool 泛左值访问 char 变量是未定义行为 #74

Open
frederick-vs-ja opened this issue Dec 31, 2024 · 1 comment · May be fixed by #80
Open

bool 泛左值访问 char 变量是未定义行为 #74

frederick-vs-ja opened this issue Dec 31, 2024 · 1 comment · May be fixed by #80

Comments

@frederick-vs-ja
Copy link
Contributor

cppguidebook/docs/undef.md

Lines 256 to 269 in 8119e85

```cpp
char c = 0;
bool b = *(bool *)&c; // 可以,b = false
```
```cpp
char c = 1;
bool b = *(bool *)&c; // 可以,b = true
```
```cpp
char c = 2;
bool b = *(bool *)&c; // 未定义行为
```

这里“可以”的代码实际上也是错误的,应该换别的写法。

@frederick-vs-ja
Copy link
Contributor Author

注意这里的未定义行为存在于左值到右值转换,见 CWG2899

@frederick-vs-ja frederick-vs-ja linked a pull request Jan 7, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant