diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 9033b51df28..70dcc20158a 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2013-02-15 Jason Merrill + + PR c++/52026 + * semantics.c (finish_id_expression): In a template, return + the identifier for a constant variable. + 2013-02-14 Jason Merrill PR c++/54922 diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index 28b4b791273..0e09d04b442 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -3015,7 +3015,14 @@ finish_id_expression (tree id_expression, FIXME update for final resolution of core issue 696. */ if (decl_constant_var_p (decl)) - return integral_constant_value (decl); + { + if (processing_template_decl) + /* In a template, the constant value may not be in a usable + form, so look it up again at instantiation time. */ + return id_expression; + else + return integral_constant_value (decl); + } /* If we are in a lambda function, we can move out until we hit 1. the context, diff --git a/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-const2.C b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-const2.C new file mode 100644 index 00000000000..d2457d6491c --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-const2.C @@ -0,0 +1,15 @@ +// PR c++/52026 +// { dg-options "-std=c++11 -O" } +// { dg-do run } + +template +int func() { + const int constVal1 = B ? 100 : -100; + const int constVal = constVal1; + return [] { return constVal; }(); +} + +int main() { + if (func() != 100) + __builtin_abort (); +}