Linked Questions
11 questions linked to/from Static constexpr odr-used or not?
309 votes
3 answers
199k views
Does static constexpr variable inside a function make sense?
If I have a variable inside a function (say, a large array), does it make sense to declare it both static and constexpr? constexpr guarantees that the array is created at compile time, so would the ...
19 votes
3 answers
2k views
Is a constexpr array necessarily odr-used when subscripted?
Given the following code: struct A { static constexpr int a[3] = {1,2,3}; }; int main () { int a = A::a[0]; int b [A::a[1]]; } is A::a necessarily odr-used in int a = A::a[0]? Note: This ...
8 votes
4 answers
2k views
In class static const ODR
I am a bit confused by the static in-class initialization of a const member. For example, in the code below: #include <iostream> struct Foo { const static int n = 42; }; // const int Foo::...
9 votes
3 answers
7k views
Undefined reference error when initializing unique_ptr with a static const
When I try to use a static const to initialize a unique_ptr, I get an "undefined reference" error. However, when I new a pointer using the same constant, the symbol seems to be magically defined. ...
8 votes
2 answers
2k views
Assign static constexpr class member to runtime variable
I know there are a lot of similar questions, but somehow different questions. It is about the following situation: #include <iostream> #include <array> template<typename T> class ...
5 votes
1 answer
4k views
Can a C++ class contain a static const std::array initialized inline in a header file?
This is what I've got: struct Foo { static std::array<double, 4> acgt_default_background_frequencies() { return {0.281774, 0.222020, 0.228876, 0.267330}; } }; But I'd prefer to not use a ...
17 votes
1 answer
517 views
Odd behavior passing static constexpr members without definitions by value
I was surprised to find that GCC and Clang disagree on whether to give me a linker error when passing a static constexpr member by value when there is no out-of-class definition: #include <...
7 votes
1 answer
776 views
Undefined symbols for architecture x86_64 linking constexpr std::array
I am getting the error in while linking my object files: #include <cstdint> #include <array> enum SystemType : uint8_t { AC, DC, HCP, EFF }; template<SystemType TYPE> struct ...
0 votes
1 answer
83 views
Passing constexpr member as a reference triggers linker error with gcc in debug or -O1 modes
The code below triggers error with gcc 'undefined reference Foo::d' and 'undefined reference Foo::i'. Strangely, it happens only if I compile it in -Og, or -O1 optimization mode, but not with -O2 or -...
2 votes
1 answer
125 views
The set of potential results of `A::a[0]` is empty according to [basic.def.odr]/2. Why is it empty?
In this answer in SO the OP says: Per basic.def.odr/2, the set of potential results of A::a[0] is empty, so A::a is odr-used by this expression. Doesn't A::a[0] satisfy the second bullet point in [...
1 vote
0 answers
56 views
Static Constexpr in Struct not found [duplicate]
I have the following simple struct, which represents an OpenGL texture: #pragma once #include <GL/glew.h> #include <string> #include "Shader.hpp" struct Texture { static constexpr ...