为什么GCC不允许我创建"内联静态std::stringstream"?

Why GCC doesn't allow me to create `inline static std::stringstream`?

本文关键字:静态 std stringstream GCC 不允许 允许我 创建 为什么      更新时间:2023-10-16

我会直接去MCVE:

#include <sstream>
struct A
{
    inline static std::stringstream ss;
};

GCC 7.2和7.1拒绝以以下错误进行编译:

错误:呼叫'std :: __ cxx11 :: basic_stringstream :: basic_stringstream()'     内联静态std :: stringstream ss;                                     ^〜在Blah的文件中包括:1:0:[with _chart = char;_traits = std :: char_traits;_Alloc = STD ::分配器]       BASIC_STRINGSTREAM(BASIC_STRINGSTREAM && __RHS)       ^~~~~~~~~~~~~~~~~/Opt/compiler-explorer/GCC-7.2.0/include/c /7.2.0/sstream:723:7:注意:候选人期望1个参数,0提供

您可以在没有任何标志的情况下重现,以及-std=c++17

clang 5.0毫无问题地编译它。

其他类(例如std::string)可以毫无问题地制作inline static

使其非内线静态删除错误。

不是GCC错误还是我缺少某些东西?

错误。减少到:

struct C { explicit C() {} };
struct A {
    inline static C c;
};

GCC初始化处理代码中某个地方的某个地方错误地将其视为副本定位上下文,该上下文忽略了明确的默认构造函数。