Can C++20 concepts be used instead of templates to avoid (const/not const) code duplication ? For example (Range has nothing to do with C++20 ranges in this example), twice the same code, one const, the other not:
I know there is the possibility to use const_cast, but I was wondering if there might be a better way using C++20 concepts ?
template <typename Pr>
vector<span<Range> > chunk_by(vector<Range>& path, const Pr& InSameChunk)
{
vector<span<Range> > chunks;
int i_prev=0;
for (int i=1;i < path.size(); i++)
{
if (!InSameChunk(path[i-1], path[i]))
{
span<Range> chunk(&path[i_prev], i - i_prev);
chunks.push_back(chunk);
i_prev=i ;
}
}
span<Range> chunk(&path[i_prev], path.size() - i_prev);
chunks.push_back(chunk);
return chunks;
}
template <typename Pr>
vector<span<Range const> > chunk_by(const vector<Range>& path, const Pr& InSameChunk)
{
vector<span<Range const> > chunks;
int i_prev=0;
for (int i=1;i < path.size(); i++)
{
if (!InSameChunk(path[i-1], path[i]))
{
span<Range const> chunk(&path[i_prev], i - i_prev);
chunks.push_back(chunk);
i_prev=i ;
}
}
span<Range const> chunk(&path[i_prev], path.size() - i_prev);
chunks.push_back(chunk);
return chunks;
}