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 has no practical significance. FYI googling didn't help.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With