[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: const qualifiers in parse tree
From: |
John W. Eaton |
Subject: |
Re: const qualifiers in parse tree |
Date: |
Thu, 8 Dec 2016 17:54:16 -0500 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Icedove/45.4.0 |
On 12/08/2016 05:40 PM, Rik wrote:
However, in some cases I ran in to problems with 'const'ness. In
pt-misc.cc, I made the following change:
+verbatim+
@@ -222,12 +215,8 @@ tree_parameter_list::variable_names (voi
{
std::list<std::string> retval;
- for (const_iterator p = begin (); p != end (); p++)
- {
- tree_decl_elt *elt = *p;
-
- retval.push_back (elt->name ());
- }
+ for (tree_decl_elt* elt : *this)
+ retval.push_back (elt->name ());
return retval;
-verbatim-
However, this loses the notion of the 'const' property. I know that
getting the name() isn't going to change anything. Should we be stricter
in pt-decl.h and mark the name method as const?
Yes, if possible. The parse tree code is old and I wasn't originally
very careful with const. So there's room for improvement there. I've
tried at various times to fix this, but ran into problems where trying
to add const to one method caused a cascade of other changes that ran
into a dead end of some function that I couldn't figure out how to make
const.
> Also, I ran in to cases
where I needed the identifier in a const manner. Should we be declaring
const/non-const versions of the ident method?
+verbatim+
tree_identifier *ident (void) { return id; }
std::string name (void) { return id ? id->name () : ""; }
-verbatim-
Maybe this goes to
+verbatim+
tree_identifier *ident (void) { return id; }
const tree_identifier *ident (void) { return id; }
Did you mean to have a const qualifier for the second function above?
std::string name (void) const { return id ? id->name () : ""; }
-verbatim-
Yes, if this is enough to fix the kind of problem that I was describing
above, then I'd say do it. But maybe also add a note that it would be
good to eliminate the non-const version at some point if possible.
jwe