octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

const qualifiers in parse tree


From: Rik
Subject: const qualifiers in parse tree
Date: Thu, 8 Dec 2016 14:40:17 -0800

jwe,

I've been replacing for loops that had iterators with the C++11 range
operator instead.  Mostly this was done for code clarity, however, there
are also likely to be some performance improvements.  As an example, in
pt-mat.cc

+verbatim+
@@ -390,12 +386,10 @@ tm_row_const::tm_row_const_rep::init
 
   first_elem = true;
 
-  for (iterator p = begin (); p != end (); p++)
+  for (const octave_value& val : *this)
     {
       octave_quit ();
 
-      octave_value val = *p;
-
       dim_vector this_elt_dv = val.dims ();
-verbatim-

In the previous code the complete copy constructor for octave_value was
called just to eventually be able to call the dims() function.  The new
code just uses a reference to the existing octave_value.  That's all fine.

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?  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; }

  std::string name (void) const { return id ? id->name () : ""; }
-verbatim-

It's all very complicated so I thought I'd check what you thought was best.

--Rik




reply via email to

[Prev in Thread] Current Thread [Next in Thread]