The following compiles fine (on g++ 8.3.0-6 and --std=c++17) with the std::stringstream being a non-static class member:
#include <iostream>
#include <sstream>
class Foo {
public:
std::stringstream ss;
};
int main() {
Foo f;
f.ss << "bar\n";
std::cout << f.ss.str();
}
But the following doesn't (same compiler, same options), where the stringstream is static inline:
#include <iostream>
#include <sstream>
class Foo {
public:
static inline std::stringstream ss;
};
int main() {
Foo::ss << "bar\n";
std::cout << Foo::ss.str();
}
My compiler tells me that std::stringstream has no default constructor, which it actually should:
error: no matching function for call to ‘std::__cxx11::basic_stringstream<char>::basic_stringstream()’
static inline std::stringstream ss;
^~
What am I doing wrong here?
This is almost certainly a compiler bug. It is a little strange that the compiler knows about the constructor overload in one case but not the other, though it might have something to do with the constructor being explicit.
This does compile with 8.3:
#include <iostream>
#include <sstream>
class Foo {
public:
static inline std::stringstream ss{};
}; //^^
int main() {
Foo::ss << "bar\n";
std::cout << Foo::ss.str();
}
PS: Also this answer comes to the conclusion that explicit is the cause https://stackoverflow.com/a/47782995/4117728.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With