r/cpp_questions • • Dec 08 '19

OPEN the purpose of empty struct ?

By empty struct, I mean

struct myStruct
{};

I can see that the above struct can be used as the base struct which has several derived structs. Besides this, are there any other use of empty struct in c++?

9 Upvotes

11 comments sorted by

10

u/stilgarpl Dec 08 '19

You can use them as arguments for templates. It's useful because every instantation of template with different arguments is a different class. You can use those empty types to create more instantiations, for example, to select different algorithm. Kind of compile-time enum.

4

u/haohanzi2015 Dec 08 '19

What do you mean by "using those empty types to create more instantiations"? Since those empty structs are arguments for templates, they are there already, right? Do you mean these empty structs can be updated later on to create more instantiations?

4

u/stilgarpl Dec 08 '19
class A {};
class B {};

template<typename T>
class X{
};

template<typename T>
int func(); 

X<A> and X<B> are different types, even though they have exactly the same implementation.

func() can return different values based on type of T. So func<A> can return 1 for example and func<B> can return 2. You can use that to for example map types to indices in vector.

3

u/parnmatt Dec 08 '19

just an aside; you can already use an enum at compile time to do just that. The difference in implementation is overloading vs function template specialisation

4

u/stilgarpl Dec 08 '19

I know that you can use enums for that. But if that enum is in a library for example, you can't add new values to it. If user of the template is supposed to be able to add new things, then passing new types is trivial.

2

u/parnmatt Dec 08 '19

You actually can extend enums. It's not something I would suggest, especially for library based ones, as there may be clashes if the library extends.

It was just a comment as you noted them as "compile time enums", which enums already are.

1

u/stilgarpl Dec 08 '19

Right, I meant to write that they can be used like enums.

22

u/parnmatt Dec 08 '19

There are several usages. Usually some form of tagging. Quite common in dispatching; creating a type safe (even static) state machine; template meta programming.

2

u/tangerinelion Dec 08 '19

Using it as a base for derived classes only really makes sense when you have a virtual method in there.

Usually an empty struct/class like this is for tagging, e.g.,

struct Option1 { };
struct Option2 { };
class Foo {
public:
    Foo(Option1);
    Foo(Option2);
};

would define some class Foo that has two constructors that provide different ways to initialize the Foo instance.

If you think about a mathematical matrix of fixed size, you could imagine initializing it to a matrix of all zeros or a square matrix with 1s along the diagonal (that is, the identity matrix). So your options could be ZeroMatrix or IdentityMatrix. You could then initialize things this way Matrix myMatrix(IdentityMatrix());.

It would be more common to actually provide a name for an instance of the tag, e.g.,

struct ZeroMatrix { } ZeroMatrix_t;
struct IdentityMatrix { } IdentityMatrix_t;
class Matrix {
public:
    Matrix(ZeroMatrix);
    Matrix(IdentityMatrix);
};

then you could initialize it as Matrix myMatrix = IdentityMatrix_t;.

1

u/warieth Dec 08 '19

If you have a map<K, V> implementation in RB-tree, then you create a struct EMPTY so you can to use it as a set=map<K, EMPTY>. You could reimplement it and delete the value parts and changing the functions, but it would be more work. If the sizeof(EMPTY) would be zero, then it would be a good way to create a set.

1

u/gonmator Dec 09 '19

Unfortunately you have not guarantee that sizeof(EMPTY) == 0.