6

I have the following MCVE:

#include <sstream>

struct A {
    static std::stringstream s;
};

std::stringstream A::s;

int main() {}

When I run clang-tidy 6.0.1 on this code I get the following warning:

static_sstream.cpp:7:22: warning: initializing non-local variable with non-const expression depending on uninitialized non-local variable 'out' [cppcoreguidelines-interfaces-global-init]
std::stringstream A::s;
                     ^

It seems that the problem lies in the fact that the constructor of std::stringstream has a parameter with the default value of std::ios_base::out. My question is, is this a real problem? And if so, what is the correct way of using a static std::stringstream in a class?

Nikola Benes
  • 2,372
  • 1
  • 20
  • 33
  • 1
    This looks like a QoI issue. Those flags are declared `constexpr` https://en.cppreference.com/w/cpp/io/ios_base/openmode. I think Clang-tidy has a bug. – StoryTeller - Unslander Monica Sep 06 '18 at 11:32
  • I just found [`static constexpr openmode out = /*implementation defined*/`](https://en.cppreference.com/w/cpp/io/ios_base/openmode). Shouldn't the `constexpr` grant "compile-time" initialization of `out` or did I mis-understand the concept? – Scheff's Cat Sep 06 '18 at 11:33
  • Interestingly, I have looked into the header files of both libstdc++ and libc++ (recent versions) and they are both missing the `constexpr` specifier, they only use `const`. – Nikola Benes Sep 06 '18 at 11:36

1 Answers1

0

MSVC 2015(windows) compiles the above code snippet without any warnings. Verified the headers and it has constexpr. Looks like an issue with Clang-tidy.

GSAT
  • 56
  • 2
  • It is, in fact, not an issue with clang-tidy, it is an issue of the standard library headers of both libc++ and libstdc++, see the comments above. – Nikola Benes Sep 12 '18 at 09:14