constexpr int a = 4;
auto& b = a;
a+= 1; // compile error
b+= 1; // compile error
Trying to make b constexpr with "a" as an int is also a compile error. constexpr int a = 4;
auto& b = a;
a+= 1; // compile error
b+= 1; // compile error
Trying to make b constexpr with "a" as an int is also a compile error.E.g., if you're implementing an iterator that takes a reference to the length of the container being iterated, and you require that that reference is constexpr, that means the container will have a fixed, compile-time-known, size.
Since you didn't reply to the other parts of the comment, I'll presume that C++ doesn't have the mutex I'm talking about (apart from this attempt[0], which I just learnt about and commented on in a different reply), and that it's not possible to have non-ref-counted shared references while statically avoiding dangling pointers.
void iter(std::vector<int>& vec) {
auto& vec_size = vec.size();
vec_size += 10; //compile error
}There are many ways of forcing compile errors for mutexes that people have built, but they all come with their own trade-offs.
I've not encountered a problem where I specifically needed "non-ref-counted shared references while statically avoiding dangling pointers", so I'm not an expert on it.
Thanks for the replies about the other stuff!