6

I'm working with with MS Visual Studio 2017, V. 15.9.8.

I am using the excellent JetBrains ReSharper Ultimate 2019.1.2 Build 191.0.20190603.142841. It gives me a warning at the indicated location:

#include <vector>
struct T
{
  std::vector<char> m;
  const char *f() const
  {
    static const char emptyData;         // ReSharper complains here
    return m.size() ? &m[0] : &emptyData;
  }
};

The message is

file.h: Static local variable of type 'const unsigned char' should be initialized. This is non-standard Microsoft C++ extension.

The warning disappears if emptyData is not const.

The warning is wrong since all static data, including constant static locals, are per the standard zero-initialized, right?

Peter - Reinstate Monica
  • 15,048
  • 4
  • 37
  • 62

2 Answers2

6

The warning is wrong since all static data, including constant static locals, are per the standard zero-initialized, right?

It's just slightly inaccurate. There is initial zero initialisation indeed, but after that the variable is default initialised. For char, default initialisation is no initialisation which in case of previous zero initialisation would leave the zero value intact. A pedantically correct message would be that constant objects (of this type) must not be default initialised.

The standard (latest draft says):

If a program calls for the default-initialization of an object of a const-qualified type T, T shall be a const-default-constructible class type or array thereof.

The program violates this rule and is ill-formed.

Note that until C++17 default initialisation was not allowed for any const qualified type.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • This restriction for chars seems odd though considering that a non-const char is perfectly ready to be used without an explicit initialization. I suppose the standard wants to prevent users from creating objects which are improperly initialized by zeroing them (after all, you cannot "fix" them if they are const, as opposed to non-consts). But the committee (rightly so) didn't want to add another byzantine detail in order to say "... unless this is a POD which is perfectly happy with zero initialization". – Peter - Reinstate Monica Jan 13 '20 at 15:36
  • @Peter-ReinstateMonica Yeah, as far as I can tell the language *could* allow default initialisation of zero-initialised const objects, but it just doesn't. Indeed, I would assume that's because they wouldn't want the complexity of one more rule for such niche use case. – eerorika Jan 13 '20 at 15:42
1

I believe it's because of the const, constants variables must be initilized, if the line is const char emptyData;, you get an error for uninitialized constvariable, so I think it's not the static modifier that is causing the problem.

There is a topic about this matter that seems interesting here.

Whether it is const static char emptyData; or static const char emptyData; the error in g++2a(GNU) compiler is:

error: uninitialized 'const emptyData' [-fpermissive]

anastaciu
  • 23,467
  • 7
  • 28
  • 53