1

I would like to initialize a constexpr variable with another variable, made visible by extern:

test.cpp

#include "test.h"
int const a[] = {1};
int const d = 4;

test.h

extern int const a[];
extern int const d;

main.cpp

#include "test.h"

int main(void) {
    constexpr const int * b = a;
    constexpr const int e = d;
}

(Only) The second line in main gives the following error:

error: the value of 'd' is not usable in a constant expression and

test.h:3:18: note: 'd' was not initialized with a constant expression

Could somebody help me to understand, why only one of the two examples fails?

pebu
  • 11
  • 1
  • 1
    Everything in a constexpr must be constexpr, extern int is not constexpr; For constexpr pointers also have a look at this : https://stackoverflow.com/questions/13499658/constexpr-initializing-with-pointers – Pepijn Kramer Nov 12 '21 at 10:49

0 Answers0