Теперь мой вопрос: почему func1 является constexpr.
Вы уверены?
Попробуйте задать значение времени компиляции, сохранив его в переменной constexpr
;например
constexpr int * a = func1(3);
Вы должны получить список ошибок / предупреждений, таких как (из моего clang ++ 3.8.1)
tmp_003-14,gcc,clang.cpp:7:11: warning: address of stack memory associated with
local variable 'b' returned [-Wreturn-stack-address]
return &b;
^
tmp_003-14,gcc,clang.cpp:11:21: error: constexpr variable 'a' must be
initialized by a constant expression
constexpr int * a = func1(3);
^ ~~~~~~~~
tmp_003-14,gcc,clang.cpp:11:21: note: pointer to 'b' is not a constant
expression
tmp_003-14,gcc,clang.cpp:6:7: note: declared here
int b = 4;
^
Ну, собственно, из моего g ++ 6.3.0 я получаютолько предупреждение
tmp_003-14,gcc,clang.cpp: In function ‘constexpr int* func1(int)’:
tmp_003-14,gcc,clang.cpp:7:7: warning: address of local variable ‘b’ returned [-Wreturn-local-addr]
int b = 4;
^