Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Constant static members not initialized inside class definition #1221

Open
vladvrabie opened this issue Aug 11, 2024 · 0 comments
Open
Labels
bug Something isn't working

Comments

@vladvrabie
Copy link

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 vladvrabie added the bug Something isn't working label Aug 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant