3

I know that in C++11, I can write

class foo {
    static constexpr const char *one = "one";
}

However, when I try to do the same for an array

class bar {
    static constexpr const float prim[4] = {2, 3, 5, 7};
}

(and reference it later on) I get an undefined reference linker error.

Is this just not possible for arrays or am I missing something in the syntax?

2
  • The declaration and usage of this example compiles fine on GCC 7.2 Commented Feb 6, 2018 at 16:13
  • Look at good answer there stackoverflow.com/questions/29397864/… Commented Feb 6, 2018 at 16:32

1 Answer 1

3

Static constexpr data member declarations aren't definitions in C++11/14, therefore you cannot odr-use prim.
To work around it, put the following statement somewhere in your cpp file as you would do with any other non-constexpr static data member:

constexpr const float bar::prim[4];

In other terms, this returns an undefined reference:

struct bar {
    static constexpr const float prim[4] = {2, 3, 5, 7};
};

int main() {
    auto *foo = bar::prim;
}

This doesn't:

struct bar {
    static constexpr const float prim[4] = {2, 3, 5, 7};
};

constexpr const float bar::prim[4];

int main() {
    auto *foo = bar::prim;
}

Because in the second case you are actually defining prim other than declaring it and thus you can get its address, use it by means of a reference, and so on...

Sign up to request clarification or add additional context in comments.

2 Comments

Is this still required in C++17, Which allows inline variables to be initialized in the class?
@RobertAndrzejuk In C++17 (I cite the standard) a function or static data member declared with the constexpr specifier is implicitly an inline function or variable. Therefore things changed with C++17 actually.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.