Converting bool to text in C++

前端 未结 15 2370
走了就别回头了
走了就别回头了 2020-12-01 04:17

Maybe this is a dumb question, but is there any way to convert a boolean value to a string such that 1 turns to \"true\" and 0 turns to \"false\"? I could just use an if st

15条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-01 04:52

    Without dragging ostream into it:

    constexpr char const* to_c_str(bool b) {
       return  
        std::array{"false", "true "}[b]
       ;
    };
    

提交回复
热议问题