boostorg / boostorg/numeric_conversion

numeric_cast() does not detect enum truncation

Open
#27 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
13
Forks
42
PR merge metrics
No merged PRs in 30d

Description

This simple program demonstrates the problem. The conversion below succeeds but the converted value is truncated. I would expect an exception in this case.

```c++
#include
#include

int main()
{
enum E : unsigned int { e=1 };
const std::uint64_t value=0x1'8000'0000;
std::cout << std::hex << boost::numeric_cast(value) << std::endl; // prints "80000000"
return 0;
}
```

The program writes 80000000 instead of 180000000 to the console, so bit 32 (when numbering them from 0) is lost.

The problem does not occur (i.e. an exception is thrown) if the value is directly cast to the underlying type "unsigned int" of the enum.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.