Skip to content

Compile failure with MSVC with /W4 /WX - ostream.h(153): warning C4127: conditional expression is constant #4314

@stefan301

Description

@stefan301

The following Code doesn't compile with 'Treat warnings as errors' and warning level 4:

#include <fmt/format.h>
#include <fmt/ostream.h>
#include <iostream>

int main()
{
	fmt::print(std::cerr, "Don't {}!", "panic");
}

compiling with cl /EHsc /std:c++20 /W4 /WX /utf-8 x.cpp /MDd /I D:\libs\fmt\include D:\libs\fmt\x64\Debug\fmtd.lib gives:

D:\libs\fmt\include\fmt/ostream.h(153): error C2220: the following warning is treated as an error
D:\libs\fmt\include\fmt/ostream.h(153): warning C4127: conditional expression is constant
D:\libs\fmt\include\fmt/ostream.h(153): note: consider using 'if constexpr' statement instead

The problem is this if in print because detail::use_utf8 is a compile time constant

if (detail::use_utf8) return vprint(os, fmt.str, vargs);

Changing print this way might solve the problem for C++17 and higher but doesn't compile with C++14

FMT_EXPORT template <typename... T>
void print(std::ostream& os, format_string<T...> fmt, T&&... args) {
  fmt::vargs<T...> vargs = {{args...}};
  if constexpr( detail::use_utf8 )
  {
          return vprint( os, fmt.str, vargs );
  }
  else
  {
          auto buffer = memory_buffer();
          detail::vformat_to( buffer, fmt.str, vargs );
          detail::write_buffer( os, buffer );
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions