C++ Logo

STD-PROPOSALS

Advanced search

Subject: [std-proposals] proposal: new const-able feature ( use "const?" , or new "constable" keyword) for method/function
From: jianping z (zjpwork_at_[hidden])
Date: 2019-12-13 00:58:21


Hi all,

I have a proposal to add a const-able feature to the language in
addition to "const" keyword. it can greatly reduce the duplicated code
written for const and non-const method/function pair.

currently, most data access method/function on class object needs const
and non-const method pair, one for const object, and another for
non-const object. for example

// example-1a.hpp : a lot of duplicated codes in each pair of
const/non-const methods
class elements
{
 Â  int elem[10];

 Â  int& operator[](size_t n) { return elem[n]; }
 Â  const int& operator[](size_t n) const { return elem[n]; }

 Â  int* data() { return elem; }
 Â  const int* data() const { return elem; }

 Â  int* begin() { return elem; }
 Â  const int* begin () const { return elem; }

 Â  int* end() { return elem+10; }
 Â  const int* end() const { return elem+10; }
}

my proposal is to extend "const" keyword with a new "const?" language
feature (preferable, or add a new const-able keyword i.e. "constable"),
and rewrite the above code with following example

// example-1b.hpp : much more concise, no more duplicated code (less
error prone)
class elements
{
 Â  int elem[10];
 Â  const? int& operator[](size_t n) const? { return elem[n]; }
 Â  const? int* data() const? { return elem; }
 Â  const? int* begin() const? { return elem; }
 Â  const? int* end() const? { return elem+10; }
}

what we need
===================
a new compiler to support "const?" (or a new const-able keyword i.e.
"constable")

how it works
===================
when new compiler encounters "const?" in a method/function
declaration/definition, it automatically expands it to 2 methods
1. first one for non-const version, with all "const?" removed from the
method/function
2. second one for const version, with all "const?" in the
method/function being replaced with "const"
as a result, the final code generated by new compiler for example-1, and
example-2 are identical in this case, but later one is more friendly for
developer.

additional information
========================
1. this feature can also work for template method/function

//example-2a.hpp
template<typename T>
const? T& max(const? T& a, const? T& b)
{
 Â  return a > b ? a : b;
}

//example-2b.hpp : code auto generated by compiler from example-2a.hpp
template<typename T>
T& max(T& a, T& b)
{
 Â  return a > b ? a : b;
}

template<typename T>
const T& max(const T& a, const T& b)
{
 Â  return a > b ? a : b;
}

2. not only parameters and return value, local variables can be
const-able too, and it can be mixed with other const parameters, local
variables
//example-3a.cpp
vector<const? int*> greater(vector<const? int*> items, const int& value)
{
 Â  const int excludedValue=12;
 Â  vector<const? int*> result;

 Â  for(const? auto item: items)
 Â  {
 Â Â Â  if(item)
 Â Â Â  {
 Â Â Â Â Â  if(*item>value && *item!=excludedValue) result.push_back(item);
 Â Â Â  }
 Â  }
 Â  return result;
}

//example-3b.cpp : code auto generated by compiler from example-3a.cpp
vector<int*> greater(vector<int*> items, const int& value)
{
 Â  const int excludedValue=12;
 Â  vector<int*> result;

 Â  for(auto item: items)
 Â  {
 Â Â Â  if(item)
 Â Â Â  {
 Â Â Â Â Â  if(*item>value && *item!=excludedValue) result.push_back(item);
 Â Â Â  }
 Â  }
 Â  return result;
}

vector<const int*> greater(vector<const int*> arr, const int& value)
{
 Â  const int excludedValue=12;
 Â  vector<const int*> result;
 Â  for(const auto item: arr)
 Â  {
 Â Â Â  if(item)
 Â Â Â  {
 Â Â Â Â Â  if(*item>value && *item!=excludedValue) result.push_back(item);
 Â Â Â  }
 Â  }
 Â  return result;
}

conclusion
===================
new const-able feature is easy to understand and easy to implement, it
can greatly reduce duplicated code (less error prone), and save
developer's time to write/read redundant code.

thanks for reading, and hopefully my proposal is useful and can work for
most developers.

Best regards,

Jianping


STD-PROPOSALS list run by std-proposals-owner@lists.isocpp.org

Standard Proposals Archives on Google Groups