cppfront icon indicating copy to clipboard operation
cppfront copied to clipboard

[BUG] Constant static members not initialized inside class definition

Open vladvrabie opened this issue 1 year ago • 1 comments
trafficstars

Describe the bug I have a Cpp1 class with some integral const static members initialized inside class. This allows me to use them to declare other members, notable std::array. Tried to port the class to Cpp2, but the static members are initialized outside the class, so I cannot use them to declare a std::array.

To Reproduce Steps to reproduce the behavior:

  1. Sample code
    My original Cpp1 code
class Piece {};
class Board {
public:
  static const size_t kWidth{ 4 };
  static const size_t kHeight{ 4 };
  static const size_t kSize{ kWidth * kHeight };
private:  std::array<std::optional<Piece>, kSize> m_pieces;
};

Cpp2 code:

Piece: type = {}
Board: type = {
  kWidth: size_t == 4;
  kHeight: size_t == 4;
  kSize: size_t == kWidth * kHeight;

  m_pieces: std::array<std::optional<Piece>, kSize> = ();
}

main: () = {
  b: Board = ();
}
  1. Command lines including which C++ compiler you are using
    For Cpp2: cppfront.exe main.cpp2 -import-std
    Visual Studio Community 2022 (64-bit) Version 17.10.5
    Microsoft (R) C/C++ Optimizing Compiler Version 19.40.33813 for x86
    Project -> Properties -> Configuration Properties -> C/C++ -> Command Line:
/JMC /experimental:module /permissive- /MP /ifcOutput "x64\Debug\" /GS /W3 /Zc:wchar_t /ZI /Gm- /Od /sdl /Fd"x64\Debug\vc143.pdb" /Zc:inline /fp:precise /D "_DEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /errorReport:prompt /WX- /Zc:forScope /RTC1 /Gd /MDd /std:c++latest /FC /Fa"x64\Debug\" /EHsc /nologo /Fo"x64\Debug\" /Fp"x64\Debug\GameCpp2.pch" /diagnostics:column 
  1. Expected result To compile to my original code or to compile to
    static constexpr size_t kWidth{ 4 }; // omitting the rest for brevity
  2. Actual result/error
    Error: Code C2975, '_Size': invalid template argument for 'std::array', expected compile-time constant expression
    Generated Cpp1 code (normally i don't use -clean, but I'm pasting here the clean version)
#define CPP2_IMPORT_STD          Yes
#include "cpp2util.h"

class Piece;
class Board;
  
class Piece {
  public: Piece() = default;
  public: Piece(Piece const&) = delete; /* No 'that' constructor, suppress copy */
  public: auto operator=(Piece const&) -> void = delete;
};

class Board {
  public: static const size_t kWidth;
  public: static const size_t kHeight;
  public: static const size_t kSize;

  private: std::array<std::optional<Piece>,kSize> m_pieces {}; 
  public: Board() = default;
  public: Board(Board const&) = delete; /* No 'that' constructor, suppress copy */
  public: auto operator=(Board const&) -> void = delete;
};

auto main() -> int;

inline CPP2_CONSTEXPR size_t Board::kWidth{ 4 };
inline CPP2_CONSTEXPR size_t Board::kHeight{ 4 };
inline CPP2_CONSTEXPR size_t Board::kSize{ kWidth * kHeight };

auto main() -> int{
  Board b {}; 
}

Additional context cppreference.com static members

vladvrabie avatar Aug 11 '24 20:08 vladvrabie