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

Defaulted functions causes initialization to 0 in constructor #1006

Closed
TurboLed opened this issue Mar 24, 2020 · 2 comments
Closed

Defaulted functions causes initialization to 0 in constructor #1006

TurboLed opened this issue Mar 24, 2020 · 2 comments
Labels

Comments

@TurboLed
Copy link

TurboLed commented Mar 24, 2020

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.

@emvivre
Copy link

emvivre commented Apr 2, 2020

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

@Groovounet
Copy link
Member

Hi,

Not initializing vector types is a behavior defined by GLSL spec which GLM follows. This is why GLM_FORCE_CTOR_INIT exist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants