Summary: | bogus error: the value of 'kName' is not usable in a constant expression | ||
---|---|---|---|
Product: | gcc | Reporter: | Paul Pluzhnikov <ppluzhnikov> |
Component: | c++ | Assignee: | Not yet assigned to anyone <unassigned> |
Status: | RESOLVED FIXED | ||
Severity: | normal | CC: | daniel.kruegler, jason, ktietz, richard-gccbugzilla, ville.voutilainen, webrown.cpp |
Priority: | P3 | Keywords: | rejects-valid |
Version: | unknown | ||
Target Milestone: | 7.2 | ||
Host: | Target: | ||
Build: | Known to work: | ||
Known to fail: | Last reconfirmed: | 2014-12-10 00:00:00 |
Description
Paul Pluzhnikov
2013-11-01 21:30:27 UTC
Hmm, issue seems to be in too restrictive decl_maybe_constant_var_p function. We could allow here additional ARRAY_TYPEs with constant, non-vla, and trivial destructors. Maybe even non-trivial destructors could be ok. Not sure Suggested patch, which allows provided testcase to run is: Index: decl2.c =================================================================== --- decl2.c (Revision 218570) +++ decl2.c (Arbeitskopie) @@ -4157,8 +4157,12 @@ decl_maybe_constant_var_p (tree decl) return false; if (DECL_DECLARED_CONSTEXPR_P (decl)) return true; - return (CP_TYPE_CONST_NON_VOLATILE_P (type) - && INTEGRAL_OR_ENUMERATION_TYPE_P (type)); + if (!CP_TYPE_CONST_NON_VOLATILE_P (type)) + return false; + return ((TREE_CODE (type) == ARRAY_TYPE + && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (type)) + && !array_of_runtime_bound_p (type)) + || INTEGRAL_OR_ENUMERATION_TYPE_P (type)); } /* Complain that DECL uses a type with no linkage. In C++98 mode this is (In reply to Kai Tietz from comment #1) > Hmm, issue seems to be in too restrictive decl_maybe_constant_var_p function. I don't know how the GCC code is structured, but I don't think that's right; that function appears to be checking whether the value of the variable can be used in a constant expression. The relevant condition here is whether the address of the variable can be used. (In reply to Richard Smith from comment #2) > (In reply to Kai Tietz from comment #1) > > Hmm, issue seems to be in too restrictive decl_maybe_constant_var_p function. > > I don't know how the GCC code is structured, but I don't think that's right; > that function appears to be checking whether the value of the variable can > be used in a constant expression. The relevant condition here is whether the > address of the variable can be used. hmm, this function has nothing to do with its value. AFAIU the comment (and its use) it just checks that a VAR-decl might be a constant. In general it checks for const and volatile attributes, and assumes for integral/enumeral typed variables that variable is constant. So a 'const char *' isn't constant - as just the destination the variable is pointing to is constant, but not the variable itself. For a constant array with trivial destructor, and non-vla size this is different. The array's name is indeed a constant address, and its content is constant too. Of course the a variable pointing to into an array isn't constant, but the array itself is. Anyway I might be wrong here |