Getting a bool reference from std::vector<bool>

送分小仙女□ 提交于 2019-11-28 14:08:31

Is there a way how to get a reference of boolean variable from std::Vector?

No.

Or any different solution?

Return typename std::vector<T>::reference instead of T&. For bool, it will return the vector's proxy type; for others, it will return a regular reference.

Or specialise A<bool> to use something other than vector<bool>.

Or use some other type (perhaps char, or a simple class wrapping a bool) instead of bool.

You hit the curse of the fake-container specialization.

That's an acknowledged design-error the standard still propagates, so you need to specialize your template to avoid the standard-specialization.

Use a std::vector<mybool> with struct mybool{bool value;}; or some such in your specialization (and curse the stubborn committee for not deprecating it fast and undoing their error by now).

Alternatively, just return std::vector<T>::reference instead of T&. (Abstain if possible, don't propagate that wart) (Don't forget proper cursing)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!