0

I know it's recommended that class declarations should be in the header file and class member initialization should be in the source file. However, since my struct object (compound) is declared as static constexpr, I believe I can define it within the header file (shown below).

header1.h:

namespace common
{
   namespace abc
   {
      struct compound
      {
         unsigned int intgr1;
         unsigned int intgr2;
         unsigned int intgr3;
      };
   }
}

test.h:

#include "header1.h"

namespace common
{
   namespace def
   {
      class xyz
      {
         // public: ...

         private:
            static constexpr abc::compound foo = 
               {0, 1, 2}; // <--- C4268 warning here
      };
   }
}

test.cpp:

#include "test.h"

namespace common
{
   namespace def
   {
      constexpr abc::compound xyz::foo;
   }
}

However, this gives me a level 4 compiler (MSVS 2015) warning (C4268 - shown above in test.h) stating:

'private: static common::abc compound common::def::xyz::foo':'const' 
static/global data initialized with compiler generated constructor fills the   
object with zeros

What does this warning mean? Is there a better way of initializing this struct?

Steve Cho
  • 431
  • 2
  • 5
  • 10
  • Question with that recommendation is rather outdated (2008) while VS2015 does not even support `constexpr` properly. With C++17 there is no need to define `foo` in `test.cpp` because `static` class fields can be inline. – user7860670 Oct 09 '18 at 12:27
  • maybe i am just a bit confused by your namespaces, but isnt `common::def::xyz::foo` (in the header) completely unrelated to `common::def::foo` (the one in the source) ? – 463035818_is_not_an_ai Oct 09 '18 at 12:27
  • @user463035818 yes I believe you are correct. Edited. – Steve Cho Oct 09 '18 at 12:40
  • I can't reproduce that warning: https://godbolt.org/z/OYFlyd (note, that gcc and clang are complaining that you are missing direct call to default constructor, but adding it doesn't change much). – Michał Łoś Oct 09 '18 at 12:47
  • @MichałŁoś I'm not familiar with godbolt but I noticed you added in curly brackets {} for the constexpr intializers in the source file. Removing that and then adding in the xyz:: scope operator (recent edit) should remove those complaints (although I don't think the warning is being reproduced on this site - perhaps because level 4 warnings aren't activated?) – Steve Cho Oct 09 '18 at 13:07
  • @SteveCho: yes, I've added them to make it compile on clang and gcc. I've added /Wall to MSVC, and it seems that in C++17 it does copy-elide as it should: https://godbolt.org/z/p3mDzf (checked on newest MSVC). On older one it seems to be a bug, because when you remove '=' in line 24 it still generates the same warning on MSVC 2015: https://godbolt.org/z/mAJflU – Michał Łoś Oct 09 '18 at 13:15
  • @MichałŁoś the '=' is not necessary. I believe initializing structs can be done with or without it. So removing it on line 24 should still generate the same warning as it changes nothing. – Steve Cho Oct 09 '18 at 13:36

0 Answers0