File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change @@ -126,3 +126,39 @@ fwd(static_cast<ProcessFuncType>(workOnVal)); //성공
126
126
```
127
127
128
128
## 비트 필드
129
+
130
+ 아래와 같이 비트 필드가 함수의 인수로 쓰일 때 어떻게 동작하는지 보자.
131
+
132
+ ```c++
133
+ struct IPv4Header {
134
+ std::uint32_t version:4,
135
+ IHL:4,
136
+ DSCP:6,
137
+ ECN:2,
138
+ totalLength:16;
139
+ …
140
+ };
141
+
142
+ void f(std::size_t sz);
143
+
144
+ IPv4Header h;
145
+ …
146
+ f(h.totalLength); // 성공
147
+ fwd(h.totalLength); // 실패
148
+ ```
149
+
150
+ ### 문제점
151
+ C++ 표준은 "비const 참조는 절대로 비트필드에 묶이지 않아야 한다"라고 명확하게 선고한다. <br >
152
+ 이러한 금지 이유는 워드의 일부분인 비트를 직접적으로 지칭하는 방법이 없기 때문이다. <br >
153
+ 포인터는 가장작은 단위가 char이고, 참조 또한 불가능하다. <br >
154
+
155
+ ### 해결책
156
+ 비트필드를 인수로 받는 임의의 함수는 그 비트필드의 값의 복사본을 받게된다.
157
+ 비트필드를 매개변수에 전달하는 2가지 방법
158
+ 1 . 값으로 전달
159
+ 2 . const에 대한 참조로 전달
160
+
161
+ ``` c++
162
+ auto length = static_cast <std::uint16_t >(h.totalLength);
163
+ fwd (length); //
164
+ ```
You can’t perform that action at this time.
0 commit comments