C++ Logo

std-proposals

Advanced search

proposal: new const-able feature ( use "const?" , or new "constable" keyword) for method/function

From: jianping z <zjpwork_at_[hidden]>
Date: Thu, 12 Dec 2019 23:58:21 -0700
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

Received on 2019-12-13 01:00:40