Design of std::ifstream class

后端 未结 3 1897
陌清茗
陌清茗 2020-12-11 20:21

Those of us who have seen the beauty of STL try to use it as much as possible, and also encourage others to use it wherever we see them using raw pointers and a

3条回答
  •  隐瞒了意图╮
    2020-12-11 20:33

    It's typically no more expensive to get a C string from a std::string than it is to construct a std::string from a C string so, given that you are likely to want to use std::ifstream with filenames that come from both, using a const char* in the interface is not a significant cost.

    Is this a serious mistake with the design?

    What can't you do with the current interface? What concrete and significant benefit would taking a const std::string& in the interface yield?

    The real benefit of a std::string overload, as I see it, is as a help to beginners making it easy to get things right when first attempting to use std::string and streams together. To experienced C++ developers the trivial cost of writing .c_str() when necessary is likely to be negligible compared to rest of the effort that goes into developing code.

提交回复
热议问题