Enforcing type safety in C ++ without using additional classes

I am a little familiar with type safety and have used it successfully earlier in methods that receive multiple parameters of the same type (bool) to avoid confusion. For example:

// Old version of the method
void sendPackage(bool sendImmediately, bool dividePacket);

// Type safe version
enum SendImmediatelyPreference
{
    SEND_IMMEDIATELY,
    DO_NOT_SEND_IMMEDIATELY
};

enum PacketDivisionPreference
{
    DIVIDE_PACKET,
    DO_NOT_DIVIDE_PACKET
};

void sendPackage(
    SendImmediateltPreference immediatePref,
    PacketDivisionPreference divisionPref);

      

So, sendPackage(true, false)

it becomes mysterious sendPackage(SEND_IMMEDIATELY, DO_NOT_DIVIDE_PACKET)

.

The problem is that this is only an option for bool

. I have a method that takes multiple std::vector<std::string>

, and I would like to minimize the possibility of the user entering the arguments in the wrong order.

I can think of creating different classes that contain std::vector<std::string>

, and either override tons of methods std::vector

or expose an inner vector.

Is there an easier way, some kind of typedef, that enforces type safety? Usage boost

will be fine.

+3


source to share


5 answers


BOOST_STRONG_TYPEDEF

is exactly a typedef that enforces type safety.



However, this answer contains some caveats related to using this strong typedef only for a function, and asserts that types should be used throughout the code to prevent unnecessary casting.

0


source


How about an alternative approach using named parameters? There are several ways in C ++ described here here . The tuple tag approach looks reasonable. There is also a boost option .

This does not provide strong type safety, but you could argue that the user might also call the wrong constructor to make his type object safe, because he must use the wrong tag when calling your function. This situation is less likely if types are used throughout the application and not just for one specific function.



See also discussion of the strong strong typedef vs parameter for a similar purpose here .

+2


source


Not sure if I understood you correctly, but maybe this can help:

enum SendImmediatelyPreference : bool  // allows only 2 options:
{
  DO_NOT_SEND_IMMEDIATELY,  // false
  SEND_IMMEDIATELY          // true
}

      

0


source


How do I create a class that inherits (public) from std :: vector in order to have a strong typecheck. The advantage is that you only need to rewrite the constructors ..

You can also rearrange your parameters in std :: unordered_map> to implement the argument as a dict (in python or javascript for example).

0


source


I like to bind parameters in a config class or struct. For example:

struct SendOptions
{
    bool send_immediately = false;
    bool divide_packet = false;
    // ...
};

void sendPackage(SendOptions options);

      

This has the added advantage that additional options can be added later without having to change the interface sendPackage(SendOptions)

.

0


source







All Articles