Defaulted functions causes initialization to 0 in constructor
TurboLed opened this issue · comments
When GLM_FORCE_CTOR_INIT is not set, with C++11, it causes the default compiler-generated constructor to be used:
#if GLM_HAS_DEFAULTED_FUNCTIONS && GLM_CONFIG_CTOR_INIT == GLM_CTOR_INIT_DISABLE
define GLM_CONFIG_DEFAULTED_FUNCTIONS GLM_ENABLE
define GLM_DEFAULT = default
#else
define GLM_CONFIG_DEFAULTED_FUNCTIONS GLM_DISABLE
define GLM_DEFAULT
#endif
The compiler-generated constructor, as per the standard, will initialize all non-static members to their default value. Meaning for POD-types, they will be initialized to 0. The above defines (GLM_CONFIG_CTOR_INIT == GLM_CTOR_INIT_DISABLE) seem to assume that the default compiler-generated constructors don't initialize members, which is false.
When not using C++11, the user-defined constructors are used, and the variables will not be initialized to 0 unless GLM_FORCE_CTOR_INIT is set. With C++11, there is no way to turn off the initialization to 0 as is it.
A fix involving disabling defaulted functions can cause the copy constructor to also be user-defined, which can trigger new compilation issues with some CUDA functions passing glm objects to device kernels.
Hello,
In my side, with C++11 or C++17 the initialization to 0 is turn off. But it's horrible because it use random values on stack, and the compiler does not detect that glm variables are not initialized.
I would prefered to have a glm library that initialize zero values by default, and propose to user the non-intialization version with a macro definition.
See the following code for an example :
$ git clone https://github.com/g-truc/glm
$ cat main.cpp
#include <iostream>
#include "glm/glm.hpp"
#include <vector>
int main()
{
glm::dvec2 v;
std::vector< glm::dvec2 > vec;
for ( glm::dvec2 p : vec ) {
v += p;
}
std::cout << v.x << " " << v.y << "\n";
return 0;
}
$ g++ -o main main.cpp -Wall -Werror -g -Iglm/ -std=c++11
$ ./main
6.92165e-310 0
$ g++ -o main main.cpp -Wall -Werror -g -Iglm/ -std=c++17
$ ./main
6.92317e-310 0
Hi,
Not initializing vector types is a behavior defined by GLSL spec which GLM follows. This is why GLM_FORCE_CTOR_INIT exist.