String string parameters

My library receives information from the message bus as strings. These strings are identifiers for the different types of objects that need to be processed.

The Currenly method might look like this:

void doWork(const std::string& carModelId,
            const std::string& dealershipId,
            const std::string& invoiceId);

float getPrice(const std::string& carModelId, 
               const std::string& dealershipId);

Since these different types of identifiers are used universally, I would like to make sure that the correct identifier type is passed to the method instead of a simple string.

My current idea is to create some structures that wrap these lines

struct carModel
{
     std::string id;
}

so that these methods accept only the correct id type

void doWork(const carModel& carModelId,
            const dealership& dealershipId,
            const invoice& invoiceId);

On the other hand, this seems to be a lot of additional pattern for a common problem.

Question : what is best suited to type safety for string parameters?

+4
1

, , IDE, .. .

struct SomeId {
    std::string id;
}

. , ( , , , ..). , , , .

, ( , ?). , , .

, , , . , .

, : . .

SomeId id = someOtherIdType;

, operator() operator-> , .

, , ,

struct BaseId
{
    std::string id;

    void Do() { }

    //add common behavior to all id classes
    //...
};

struct myId : private BaseId
{
    using BaseId::id;
};

, , , id.

, , .. .

? . , , . . , , ( typedef , , ).

+3

All Articles