0

Is there a reason why a fully initialized constexpr member have to be defined again outside the class declaration ?

Isn't it redundant since it's static, constant and already fully initialized inside the declaration ?

Here is an example. I am using C++17

#include <iostream>

struct Settings {
    static constexpr const char * str[2] = {"Hello 1", "Hello 2"};
};

void print(int i) {
    printf("%s\n", Settings::str[0]); // Does not need a definition
    printf("%s\n", Settings::str[i]); // Needs a definition
}

int main() {
    print(1);
    return 0;
}

The line :

printf("%s\n", Settings::str[i]);

generate this error :

undefined reference to `Settings::str'

But not the line :

printf("%s\n", Settings::str[0]);

Thanks

infiniteLoop
  • 383
  • 2
  • 12
  • 1
    If you are *really* using C++17 it should not be needed, otherwise [Understanding static constexpr member variables](http://stackoverflow.com/questions/34053606/understanding-static-constexpr-member-variables) – Bo Persson Apr 29 '17 at 19:02
  • works fine with a c++17 compiler – deW1 Apr 29 '17 at 19:05
  • I am using -std=c++17 with GCC 6.3.0. Maybe this feature is not already implemented in my version. Do you have the name the feature ? – infiniteLoop Apr 29 '17 at 19:08
  • I believe this was changed very late in the C++17 revision, long after gcc 6 was released. Formally everything is still "experimental" as the C++17 standard is still out on a formal vote by the ISO member countries. – Bo Persson Apr 29 '17 at 19:12
  • Ok, thank you for your help. I will try to find an updated version of gcc. It is a good thing they fixed this in the standard because it was a kind of code duplication for no reason – infiniteLoop Apr 29 '17 at 19:17

0 Answers0