Advantages of an empty class in C++

后端 未结 9 1551
夕颜
夕颜 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:27

    "empty" classes means classes which have no data members? They typically declare typedefs or member functions, and you can extend them with your own classes.

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-08 21:33

    You can use it like a placeholder for checking purpose or as enabler to special functionality. For example in Java exist the "empty" interface Serializable used to specify if a class is serializable.

    0 讨论(0)
  • 2020-12-08 21:34

    In the STL, Standard Template Library of the C++, for example you have

    template<class _Arg,
     class _Result>
    struct unary_function
        { // base class for unary functions
     typedef _Arg argument_type;
     typedef _Result result_type;
        };
    

    When defining a functor, you can inherit unary_function, and then you have the typedef defined automatically at your disposal.

    0 讨论(0)
  • 2020-12-08 21:37

    The following can be used to have a boost::variant which can hold an (SQL) NULL value for example.

    class Null { };
    
    typedef boost::variant<Null, std::string, int> Value;
    

    To make it more useful things like operator== and operator<< are handy. For example:

    std::ostream& operator<<(std::ostream &lhs, const Null &rhs)
    {
         lhs << "*NULL*";
         return lhs;
    }
    
    int main()
    {
        Variant v("hello");
        std::cout << v << std::endl;
        v = Null();
        std::cout << v << std::endl;
        ...
    }
    

    Will give:

    hello
    *NULL*
    
    0 讨论(0)
  • 2020-12-08 21:51

    One use would be in template (meta-)programming: for instance, iterator tags are implemented as empty classes. The only purpose here is to pass around information at compilation time so you can check, if an iterator passed to e.g. a template function meets specific requirements.

    EXAMPLE:

    This is really simplified, just to ge an idea. Here the purpose of the tag class is to decide, which implementation of an algorithm to use:

    class forward_iterator_tag {};
    class random_access_iterator_tag {};
    
    class MySimpleForwardIterator {
    public:
      typedef typename forward_iterator_tag tag;
      // ...
    };
    
    class MySimpleRandomIterator {
    public:
      typedef typename random_access_iterator_tag tag;
      // ...
    };
    
    template<class iterator, class tag>
    void myfunc_int(iterator it, tag t) {
      // general implementation of myfunc
    }
    
    template<class iterator>
    void myfunc_int<iterator, forward_iterator_tag>(iterator it) {
      // Implementation for forward iterators
    }
    
    template<class iterator>
    void myfunc_int<iterator, random_access_iterator_tag>(iterator it) {
      // Implementation for random access iterators
    }
    
    template<class iterator>
    void myfunc(iterator it) {
      myfunc_int<iterator, typename iterator::tag>(it);
    }
    

    (I hope I got this right, it's been a while since I used this ...)

    With this code, you can call myfunc on an arbitrary iterator, and let the compiler choose the correct implementation depending on the iterator type (i.e. tag).

    0 讨论(0)
提交回复
热议问题