简体   繁体   English

自定义 fmt::formatter 中的 -Wstringop-overflow 编译器警告<std::bitset<n> > 专业化</std::bitset<n>

[英]-Wstringop-overflow compiler warning in custom fmt::formatter<std::bitset<N>> specialization

I am trying to create a specialization of fmt::formatter for std::bitset class. However I'm getting an ambiguos warning message from GCC when compiling the below program :我正在尝试为std::bitset class 创建fmt::formatter的专业化。但是,在编译以下程序时,我从 GCC 收到一条不明确的警告消息:

#include <bitset>
#include <cstddef>
#include <fmt/core.h>


template <std::size_t N>
struct fmt::formatter< std::bitset<N> > : fmt::formatter< const char* >
{
/*  constexpr auto parse( format_parse_context& ctx )
    {
        return ctx.begin( );
    }*/

    template <typename FormatContext>
    auto format( const std::bitset<N>& value, FormatContext& ctx )
    {
        return fmt::format_to( ctx.out( ), "{}", value.to_string( ) );
    }
};


int main( )
{
    std::bitset<8> bitset1 { 0b01010101 };
    fmt::print( "bitset1: {}\n", bitset1 );
}

Part of the warning message:警告信息的一部分:

warning: writing 1 byte into a region of size 0 [-Wstringop-overflow=]

How can I fix the issue (if there's actually one)?我该如何解决这个问题(如果确实存在的话)?

Note: I have commented out the parse method and instead inherited from the fmt::formatter<const char*> specialization.注意:我已经注释掉了parse方法,而是继承自fmt::formatter<const char*>专业化。 This way I don't have to implement the parse method.这样我就不必实现parse方法。 It compiled without any warnings prior to inheriting from that specialization and commenting out parse .在继承该专业化并注释掉parse之前,它在没有任何警告的情况下编译。

This warning is a known false positive in some versions of gcc. You can do any of the following to make it go away:此警告在 gcc 的某些版本中是已知的误报。您可以执行以下任一操作以使其 go 消失:

  • Suppress the warning.抑制警告。
  • Switch to a different version of gcc where the warning doesn't occur (see eg https://godbolt.org/z/YhdKv4vzM ).切换到不会出现警告的 gcc 的不同版本(参见例如https://godbolt.org/z/YhdKv4vzM )。
  • Use the latest {fmt} from github.使用来自 github 的最新 {fmt}。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM