endorph endorph - 2 months ago 10
C++ Question

Enforcing desired relationship between template function parameters

I'm working on improving an in-house messaging library, designed to send messages internally within our applications, and to external consumers. A message consists of a

MessageType
(
enum class
) and some data (
struct
). Each
MessageType::
corresponds to a particular data type (e.g.,
MessageType::TypeA
will always contain
Foo
). However, multiple message types could use the same struct (e.g.,
MessageType::TypeM
could also use
Foo
).

We have a class that can send messages. Our previous implementation of the message sender class defines a method for each type:

SendMessageTypeA(Foo data)
SendMessageTypeB(Bar data)
SendMessageTypeM(Foo data)


When there are lots of messages, this can result in a lot of code duplication (the method body is essentially the same, with the exception of the different parameter types).

I've implemented a new method:

template<typename structType>
void Send(MessageType msgType, const structType & messageData)


This single method can send any message, depending on the appropriate template parameter being provided. Note that the
MessageType
is always known at compile time.

The problem is that this new method does not enforce the relationship between
MessageType
and
struct
. For example,
Send<Foo>(MessageType::TypeB, data)
will compile, even though
MessageType::TypeB
should contain
Bar
. The mismatch will be detected at runtime, but I'd like to make it a compile time error.

I'm not sure how to achieve this. I've considered:


  1. Declaring all the
    SendMessageX()
    methods, and use them to call
    Send<MessageX>()
    . This does reduce the duplication, but I still have to create a new method every time a message is defined.

  2. Attempting to use
    static_assert
    to catch the mismatch. I'm not sure how to map
    MessageType
    s to their desired
    struct
    .

  3. I'm barking up the wrong tree


Answer

If you can lift the enum to a compile time constant, then it's possible:

template <MessageType E, class Data>
void Send(Data const& ) { ... }

We can create a class template, specialized on each enum with what that enum expects:

template <MessageType E> struct expected_type;
template <> struct expected_type<MessageType::TypeA> { using type = Foo; };
template <> struct expected_type<MessageType::TypeB> { using type = Bar; };
template <> struct expected_type<MessageType::TypeM> { using type = Foo; };

template <MessageType E>
using expected_type_t = typename expected_type<E>::type;

And then we can use that to just write that static assert:

template <MessageType E, class Data>
void Send(Data const& ) {
    static_assert(std::is_same<Data, expected_type_t<E>>{}, "!");
    // ...
}

Alternatively, could use that class template to set the Data type directly:

template <MessageType E>
void Send(expected_type_t<E> const& ) {
    ...
}