关于C++中类定义的问题":1"

Lei*_*Mou 8 c++ clang

可能重复:
'unsigned temp:3'表示什么

我在阅读Clang的代码时遇到了一个问题.

class LangOptions {
public:
    unsigned Trigraphs         : 1;  // Trigraphs in source files.
    unsigned BCPLComment       : 1;  // BCPL-style '//' comments.
    ...
};
Run Code Online (Sandbox Code Playgroud)

这是我第一次看到语法":1",":1"代表什么?谢谢!

Mac*_*cke 7

这是一个位域,这意味着该值只使用一位,而不是32位(或sizeof(unsigned) * <bits-per-byte>平台上的任何东西).

Bitfields对于编写紧凑的二进制数据结构非常有用,尽管它们具有一些性能成本,因为编译器/ CPU无法更新单个位,但在读/写完整字节时需要执行AND/OR操作.