C++ Logo

STD-DISCUSSION

Advanced search

Subject: bitfields member type access
From: Mohamed Khaled (mkmostafa92_at_[hidden])
Date: 2019-04-23 04:29:36


```
#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



STD-DISCUSSION list run by std-discussion-owner@lists.isocpp.org

Older Archives on Google Groups