Using std::map where V has no usable default constructor

后端 未结 9 1626
予麋鹿
予麋鹿 2020-12-05 13:11

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 constructo

9条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-05 13:37

    Your V doesn't have a default constructor, so you cannot really expect std::map std::map::operator[] to be usable.

    A std::map > does have a mapped_type that is default-constructible, and likely has the semantics you want. Refer to the Boost.Optional documentation for details (you will need to be aware of them).

提交回复
热议问题