Advantages of an empty class in C++

后端 未结 9 1644
夕颜
夕颜 2020-12-08 20:51

What could be the possible advantages/uses of having an empty class?

P.S: This question might sound trivial to some of you but it is just for learning purpose and h

9条回答
  •  没有蜡笔的小新
    2020-12-08 21:30

    As others have said, often an empty class (or struct) is used a placeholder, a differentiator, a token, etc.

    For example, a lot of people are unaware that there are "nothrow" versions of operator new. The syntax to invoke nothrow new is:

    p = new(std::nothrow) Bar;
    

    and std::nothrow is defined simply as

    struct nothrow_t {}; //defined in namespace std
    

提交回复
热议问题