First let me ask a general question:
I have been in this situation a few times.
That is,
I designed a class that due to unacceptable performance requirements, it was basically immutable.
Immutable in the sense that its instance can defined at construction and never modified.
In effect, all its member function were const.
(Well, there is a side question as it is whether it can be assignable still, but let's go on).
It would be nice to be able to force the use to declare the instances const to make the intent more evident.
I don't think C++ supports this without adding a level of indirection, that is to access the object through a const pointer and disabling direct construction. (Or even better a unique_ptr).
struct A{ // this class is immutable, so in princple `A a` and `A const a` should behave the same
int f() const{return 5;}
private:
A(){}
public:
template
static A const* make_const(Args&&... args){return new A(std::forward(args)...);}
};
int main(){
std::unique_ptr a(A::make_const());
std::cout << a->f() << std::endl;
}
Note that the following doesn't compile, as intended:
A a; // doesn't compile because the constructors are private
nor does
std::unique a(A::make_const()); // make creates pointers to const, so this doesn't compile.
Only the following compiles, and effectively you are mandating const:
std::unique_ptr a(A::make());
One can make it better by using std::indirect http://open-std.org/JTC1/SC22/WG21/docs/papers/2016/p0201r1.pdf and in the future overloading operator. will also help.
Of course a much cheaper alternative is to give the name of the class have const in the name like struct A_const{... or struct cA{... to hint that only const makes sense.
(Perhaps, If you decide to have mutating members later, you can inherit from this, struct A : A_const{...).