Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't std::stringstream be default constructed when it's static inline?

Tags:

c++

c++17

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?

like image 301
nada Avatar asked Apr 01 '26 20:04

nada


1 Answers

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.

like image 195
463035818_is_not_a_number Avatar answered Apr 04 '26 09:04

463035818_is_not_a_number



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!