Linked Questions
40 questions linked to/from Undefined reference to static constexpr char[]
17 votes
2 answers
3k views
Constant expression initializer for static class member of type double
In C++11 and C++14, why do I need constexpr in the following snippet: class Foo { static constexpr double X = 0.75; }; whereas this one produces a compiler error: class Foo { static const ...
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::...
12 votes
1 answer
7k views
Why does constexpr static member (of type class) require a definition?
==> See the full snippet code and compilation on coliru. I have a LiteralType class filling constexpr requirements: struct MyString { constexpr MyString(char const* p, int s) : ptr(p), sz(s) {} ...
4 votes
2 answers
2k views
Undefined reference to static constexpr string (except if it's a pointer)
This work: template<typename T> struct Something { static constexpr const char* str = "int"; }; int main() { std::cout << Something<int>::str << std::endl; } But it doesn't: ...
4 votes
2 answers
3k views
C++ equivalent of #define for integers
I am looking for a portable one line replacement for the #define in the following code. The replacement should hide the word APPLE in the namespace of the Foo object. class Foo { public: #define ...
2 votes
2 answers
3k views
Why can't a static constexpr member variable be passed to a function?
The following code produces an undefined reference to 'Test::color'. #include <iostream> struct Color{ int r,g,b; }; void printColor(Color color) { //printing color } class Test { ...
1 vote
1 answer
2k views
constexpr: definition and declaration for constexpr members
If I want to use some convenience stuff like make_array I have no chance to declare my array first and later make the definition as done in "earlier" times because the type of my var is not available ...
3 votes
3 answers
1k views
Undefined reference to static const double when used with complex.h
Here's the minimum code: #include <iostream> #include <complex> using namespace std; class Test { static const double dt = 0.1; public: void func(); }; void Test::func() { ...
2 votes
3 answers
2k views
Undefined reference to static variable of a class template
below is my code: // types.h template <typename T> struct type_to_char {}; template <> struct type_to_char<char> { static constexpr char str[] = "baz"; }; // main.cpp #include &...
2 votes
1 answer
580 views
constexpr static data member giving undefined reference error
I'm working on a kernel and I want to make my static data member constexpr so I can have its values in an enum class. However, if I do so I get an undefined reference error. It only seems to work if I ...
5 votes
2 answers
329 views
undefined reference to class static constexpr struct, g++ vs clang
This is my code, a.cpp struct int2 { int x, y; }; struct Foo{ static constexpr int bar1 = 1; static constexpr int2 bar2 = {1, 2}; }; int foo1(){ return Foo::bar1; // this is ok for ...
3 votes
0 answers
1k views
C++ constexpr member pointers in classes
I'm trying to use a class to group all the parameters of a template data structure, in particular an intrusive AVL tree. The user would do something like this: struct MyEntry { MyEntry *parent; ...
3 votes
1 answer
691 views
Can I mix compile time string comparison with MPL templates?
I got this compile time string comparison from another thread using constexpr and C++11 (http://stackoverflow.com/questions/5721813/compile-time-assert-for-string-equality). It works with constant ...
6 votes
1 answer
410 views
constexpr array member with template specialization: inconsistent behavior cross compilers
Consider the following code: #include <iostream> template<class T> struct foo {}; template<> struct foo<int> { static constexpr char value[] = "abcde"; }; ...
1 vote
1 answer
504 views
Undefined reference to initialized static member variable with make_shared
Compiling with -std=c++14 the following code: #include <memory> class A { public: static constexpr int c = 0; std::shared_ptr<int> b; A() { b = std::make_shared&...