【问题标题】:Why GCC doesn't allow me to create `inline static std::stringstream`?为什么 GCC 不允许我创建`inline static std::stringstream`?
【发布时间】:2018-05-26 10:48:18
【问题描述】:

我会直接去 MCVE:

#include <sstream>

struct A
{
    inline static std::stringstream ss;
};

GCC 7.2 和 7.1 refuse to compile 出现以下错误:

错误:没有匹配函数调用 'std::__cxx11::basic_stringstream::basic_stringstream()' 内联静态 std::stringstream ss; ^~ 在 blah:1:0 包含的文件中: /opt/compiler-explorer/gcc-7.2.0/include/c++/7.2.0/sstream:723:7:注意:候选:std::__cxx11::basic_stringstream::basic_stringstream(std::__cxx11::basic_stringstream&&) [与_CharT = char; _Traits = std::char_traits; _Alloc = std::allocator] 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 错误还是我遗漏了什么?

【问题讨论】:

  • 您使用什么编译器选项? IIRC,inline 变量是针对 C++17 提出的。您使用的 GCC 版本是否需要 -std=c++17 之类的内容?
  • @AndrewHenle 无论有没有-std=c++17,它都不起作用,不需要其他标志来重现。正如我所说,其他类型的内联静态变量工作正常。
  • 可能的解决方法:static inline std::stringstream ss{};?
  • @AndrewHenle C++17 已经发布

标签: c++ gcc stringstream


【解决方案1】:

错误。缩减为:

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

GCC 的初始化处理代码中的某处错误地将其视为忽略显式默认构造函数的复制初始化上下文。..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-03
    • 2014-01-02
    • 2013-02-13
    • 2010-12-08
    • 2014-12-14
    • 2015-10-06
    • 1970-01-01
    相关资源
    最近更新 更多