numeric_cast() does not detect enum truncation
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 35/100
Research direction
Start by compiling and running the reproducer against boost/numeric/conversion/cast.hpp, then trace the numeric_cast() conversion path for enum targets. Done means the supplied conversion rejects the overflowing value with an exception and regression coverage demonstrates that behavior.
Written by the indexing model from the issue text.
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.
#include <iostream>
#include <boost/numeric/conversion/cast.hpp>
int main()
{
enum E : unsigned int { e=1 };
const std::uint64_t value=0x1'8000'0000;
std::cout << std::hex << boost::numeric_cast<E>(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.
- Dominant language
- C++
- Stars
- 13
- Forks
- 42
- PR merge metrics
- No merged PRs in 30d
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from boostorg/numeric_conversion
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
All issues in boostorg/numeric_conversion
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
flutter-webrtc/flutter-webrtc#2206 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
google-ai-edge/LiteRT-LM#3739 ·
-
Component: GLib
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
-
Mute ydb/tests/functional/dstool/test_canonical_requests.py.Test.test_group_take_snapshot in main Openai_reviewed
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
ydb-platform/ydb#53974 · 3 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
google/libultrahdr#485 ·