I have a symbol table implemented as a std::map
. For the value, there is no way to legitimately construct an instance of the value type via a default constructor. However if I don't provide a default constructor, I get a compiler error and if I make the constructor assert, my program compile just fine but crashes inside of map<K,V>::operator []
if I try to use it to add a new member.
Is there a way I can get C++ to disallow map[k]
as an l-value at compile time (while allowing it as an r-value)?
BTW: I know I can insert into the map using Map.insert(map<K,V>::value_type(k,v))
.
Edit: several people have proposed solution that amount to altering the type of the value so that the map can construct one without calling the default constructor. This has exactly the opposite result of what I want because it hides the error until later. If I were willing to have that, I could simply remove the assert from the constructor. What I Want is to make the error happen even sooner; at compile time. However, it seems that there is no way to distinguish between r-value and l-value uses of operator[]
so it seems what I want can't be done so I'll just have to dispense with using it all together.