Skip to content

numeric_cast() does not detect enum truncation #27

@pkl97

Description

@pkl97

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions