Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

numeric_cast() does not detect enum truncation

Open
#27 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
cpp
Domain
tooling

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from boostorg/numeric_conversion

All issues in boostorg/numeric_conversion

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.