Skip to content

Test marking single-argument constructor as constexpr #3316

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

Closed

Conversation

scpeters
Copy link
Contributor

@scpeters scpeters commented Apr 7, 2025

This adds an additional commit (09f921e) on top of #3315 to test a suggestion in #3315 (comment).

This fails to compile on my local machine:

OgreMain/src/OgreVector.cpp:34:19: error: constexpr variable 'gVector2Zero' must be initialized by a constant expression
   34 | constexpr Vector2 gVector2Zero( 0 );
      |                   ^~~~~~~~~~~~~~~~~
OgreMain/include/OgreVector.h:304:28: note: non-constexpr constructor 'VectorBase' cannot be used in a constant expression
  304 |         explicit constexpr Vector(T s)
      |                            ^
OgreMain/src/OgreVector.cpp:34:19: note: in call to 'Vector(0.000000e+00)'
   34 | constexpr Vector2 gVector2Zero( 0 );
      |                   ^
OgreMain/include/OgreVector.h:70:9: note: declared here
   70 |         VectorBase() {}
      |         ^
OgreMain/src/OgreVector.cpp:39:19: error: constexpr variable 'gVector2UnitScale' must be initialized by a constant expression
   39 | constexpr Vector2 gVector2UnitScale( 1 );
      |                   ^~~~~~~~~~~~~~~~~~~~~~
OgreMain/include/OgreVector.h:304:28: note: non-constexpr constructor 'VectorBase' cannot be used in a constant expression
  304 |         explicit constexpr Vector(T s)
      |                            ^
OgreMain/src/OgreVector.cpp:39:19: note: in call to 'Vector(1.000000e+00)'
   39 | constexpr Vector2 gVector2UnitScale( 1 );
      |                   ^
OgreMain/include/OgreVector.h:70:9: note: declared here
   70 |         VectorBase() {}
      |         ^
OgreMain/src/OgreVector.cpp:41:19: error: constexpr variable 'gVector3Zero' must be initialized by a constant expression
   41 | constexpr Vector3 gV[ 94%] Building CXX object OgreMain/CMakeFiles/OgreMain.dir/src/OgreVertexIndexData.cpp.o
ector3Zero( 0 );
      |                   ^~~~~~~~~~~~~~~~~
OgreMain/include/OgreVector.h:304:28: note: non-constexpr constructor 'VectorBase' cannot be used in a constant expression
  304 |         explicit constexpr Vector(T s)
      |                            ^
OgreMain/src/OgreVector.cpp:41:19: note: in call to 'Vector(0.000000e+00)'
   41 | constexpr Vector3 gVector3Zero( 0 );
      |                   ^
OgreMain/include/OgreVector.h:130:9: note: declared here
  130 |         VectorBase() {}
      |         ^
OgreMain/src/OgreVector.cpp:48:19: error: constexpr variable 'gVector3UnitScale' must be initialized by a constant expression
   48 | constexpr Vector3 gVector3UnitScale( 1 );
      |                   ^~~~~~~~~~~~~~~~~~~~~~
OgreMain/include/OgreVector.h:304:28: note: non-constexpr constructor 'VectorBase' cannot be used in a constant expression
  304 |         explicit constexpr Vector(T s)
      |                            ^
OgreMain/src/OgreVector.cpp:48:19: note: in call to 'Vector(1.000000e+00)'
   48 | constexpr Vector3 gVector3UnitScale( 1 );
      |                   ^
OgreMain/include/OgreVector.h:130:9: note: declared here
  130 |         VectorBase() {}
      |         ^
OgreMain/src/OgreVector.cpp:50:19: error: constexpr variable 'gVector4Zero' must be initialized by a constant expression
   50 | constexpr Vector4 gVector4Zero( 0 );
      |                   ^~~~~~~~~~~~~~~~~
OgreMain/include/OgreVector.h:304:28: note: non-constexpr constructor 'VectorBase' cannot be used in a constant expression
  304 |         explicit constexpr Vector(T s)
      |                            ^
OgreMain/src/OgreVector.cpp:50:19: note: in call to 'Vector(0.000000e+00)'
   50 | constexpr Vector4 gVector4Zero( 0 );
      |                   ^
OgreMain/include/OgreVector.h:259:9: note: declared here
  259 |         VectorBase() {}
      |         ^

scpeters added 3 commits April 6, 2025 20:55
This fixes the initialization-order-fiasco for static
Vector constants while preserving API (though breaking
ABI) through the following steps:

* Make constexpr the VectorBase and Vector constructors
* Instantiate global constexpr variables in a hidden
  namespace in .cpp file
* Change static class constants to references (ABI break)
  and assign to global constexpr variables

This matches the approach taken in OGRECave#3313. Note that
the Vector(T) constructor could not be made constexpr,
so the ZERO and SCALE variables were constructed with
repeated arguments instead of a single argument.
This fails to compile on my machine.
@scpeters
Copy link
Contributor Author

scpeters commented Apr 7, 2025

09f921e causes build failures, so I tried marking more constructors as constexpr in 57d2bbf, which also fails

@paroj
Copy link
Member

paroj commented Apr 7, 2025

so I tried marking more constructors as constexpr in 57d2bbf, which also fails

yeah.. apparently we need C++20 for this

@scpeters scpeters closed this Apr 7, 2025
@scpeters scpeters deleted the init_order_fiasco_vector_test branch April 7, 2025 23:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants