Date: Tue, 23 Apr 2019 11:29:36 +0200
```
#include <type_traits>
#include <iostream>
struct C
{
uint32_t x : 2;
bool y : 2;
};
void f(int)
{
std::cout << "void f(int)" << std::endl;
}
void f(uint32_t)
{
std::cout << "void f(uint32_t)" << std::endl;
}
int main()
{
C c{0b1};
std::cout << (static_cast<uint32_t>(0b1) << 31) << std::endl;
std::cout << (c.x << 31) << std::endl;
std::cout << (c.x << 10) << std::endl;
f(c.x);
std::cout << std::boolalpha << std::is_same_v<decltype(c.x), uint32_t>
<< std::endl;
std::cout << std::boolalpha << std::is_same_v<decltype(c.y), bool> <<
std::endl;
}
```
Output
```
2147483648
-2147483648
1024
void f(uint32_t)
true
true
```
Hi all,
My question is regarding the return type of accessing bitfields. I have
learned that for built-in arithmetic operators, numeric promotion takes
place instead of using the type declared inclass. I find this very
confusing that c.x is treated as int despite being declared as uin32_t and
to add more to the confusion the static type check resolves to uint32 and
naturally the overload resolution. Is there a reason for this inconsistent
behaviour?
Regards,
Mohamed
#include <type_traits>
#include <iostream>
struct C
{
uint32_t x : 2;
bool y : 2;
};
void f(int)
{
std::cout << "void f(int)" << std::endl;
}
void f(uint32_t)
{
std::cout << "void f(uint32_t)" << std::endl;
}
int main()
{
C c{0b1};
std::cout << (static_cast<uint32_t>(0b1) << 31) << std::endl;
std::cout << (c.x << 31) << std::endl;
std::cout << (c.x << 10) << std::endl;
f(c.x);
std::cout << std::boolalpha << std::is_same_v<decltype(c.x), uint32_t>
<< std::endl;
std::cout << std::boolalpha << std::is_same_v<decltype(c.y), bool> <<
std::endl;
}
```
Output
```
2147483648
-2147483648
1024
void f(uint32_t)
true
true
```
Hi all,
My question is regarding the return type of accessing bitfields. I have
learned that for built-in arithmetic operators, numeric promotion takes
place instead of using the type declared inclass. I find this very
confusing that c.x is treated as int despite being declared as uin32_t and
to add more to the confusion the static type check resolves to uint32 and
naturally the overload resolution. Is there a reason for this inconsistent
behaviour?
Regards,
Mohamed
Received on 2019-04-23 04:31:24