'=': cannot convert from 'int' to 'std::_Iosb<int>::_Fmtflags'

59 views Asked by At

Trying to use ios format flags to format the string representation of Boost multiprecision cpp_int. Everything works with gcc standard c++17, but in Visual Studio 2019 (v142) same standard, I'm getting a compilation error when using '|' operator to combine flags:

string format_cpp_int(boost::multiprecision::cpp_int& n, bool show_base, bool uppercase)
{
    auto f = std::ios::hex;
    if (show_base) f = f | std::ios::showbase;
    if (uppercase) f = f | std::ios::uppercase;
    return n.str(0, f);
}

Getting the following error:

Severity Code Description Project File Line Suppression State Error C2440 '=': cannot convert from 'int' to 'std::_Iosb::_Fmtflags' (compiling source file main.cpp)

Is there some difference between c++17 support in gcc and VS that i'm not aware of?

Also trying to cast the result back to fmtflags doesn't seem to work either.

f = static_cast<std::ios::fmtflags>(f | std::ios::showbase);

is there another way to combine the flags and pass to cpp_int?

1

There are 1 answers

0
Max He On

Because the keyword auto may get the underlying type, which may be implemented by the specific compiler.

You should write the type immediately:

std::ios::fmtflags f = std::ios::hex;
if (show_base) f = f | std::ios::showbase;
if (uppercase) f = f | std::ios::uppercase;