Why in c++ standard ( I look at cpp reference site) two variants with the same signature are allowed?
For example:
reference front();
const_reference front() const;
Why in c++ standard ( I look at cpp reference site) two variants with the same signature are allowed?
For example:
reference front();
const_reference front() const;
That trailing const
is part of the signature. Pretend the implicit this
is explicit:
reference front( This *this);
const_reference front(const This *this);
Clearly the argument lists are different.
two variants with the same signature
That's a common misconception. The const
at the end of the signature is part of the signature.
It can be useful, for example, with container classes that return references to the contained data. The const
version returns a const
reference so the datum cannot be modified through that reference.
Because the const
in the second function counts as part of the signature.
const_reference front() const;
// ^^^^^