motivation for this is that (rarely)I have a need to know that the input parameter of the class constructor or function in general is const. Usually when the class is a helper that “automates” some procedure.
Example:
Is this the OK scoped way to get random element from the container?
If you look at the code it is clear that if the container passed to constructor is changed later class functionality is broken. So is there a way to have a function “demand” const instead of “promise” const.
Example:
int f(const vector<int>& v)
{
return v.size();
}
int main()
{
vector<int> v;
v.push_back(42); // can f be changed to reject v because it is not const
cout << f(v);
}
Declare but do not implement a non-const version.
Attempts to pass a non-const vector will be resolved to this function, and then you will get a linker error because no such function exists.
Some fancy template games may be able to turn it into a compile-time error.