Is it safe to use negative integers with size_t?

后端 未结 4 1681
旧巷少年郎
旧巷少年郎 2021-02-20 17:52

I just saw some C++ code like this. It was using a condition to decide whether to walk forward or backward through a std::vector. The compiler doesn\'t complain, b

4条回答
  •  没有蜡笔的小新
    2021-02-20 18:44

    Whenever I need to deal with signed types, I always use:

    typedef std::make_signed::type ssize_t; // Since C++11
    

    ...as a signed alternative to std::size_t.

    I appreciate this question is a few years old, but I'm hoping that will help others. Credit to moodycamel::ConcurrentQueue.

提交回复
热议问题