Home >Backend Development >C++ >How Can I Implement Strongly Typed Identifiers with Type Checking in C 17?
Implementation of Strongly Typed Identifiers with Type Checking
In C 17, you can implement strongly typed identifiers using custom classes that inherit from the intended base type. This allows for type checking during compilation, preventing the assignment of incompatible values.
Consider the following code:
#include <iostream> #include <string> #include <map> struct portal_tag {}; struct cake_tag {}; template<class Tag> struct string_id : public std::string { string_id(std::string s) : _value(std::move(s)) {} const std::string& value() const { return _value; } private: std::string _value; }; // Type aliases for convenience using PortalId = string_id<portal_tag>; using CakeId = string_id<cake_tag>; int main() { PortalId portal_id("2"); CakeId cake_id("is a lie"); std::map<CakeId, PortalId> p_to_cake; // OK p_to_cake[cake_id] = portal_id; // OK // p_to_cake[portal_id] = cake_id; // Compile error // portal_id = cake_id; // Compile error // portal_id = "1.0"; // Compile error portal_id = PortalId("42"); // OK // ... Additional operations... }
In this code, string_id is a template class that inherits from std::string. It provides a value() method to access the underlying string value. Different types are used as tags to create unique identifiers (e.g., portal_tag and cake_tag).
Using this approach, assignments between identifiers of different types are prevented by the compiler. Additionally, it supports standard operations like comparison and hashing, allowing for seamless integration with containers such as maps.
The above is the detailed content of How Can I Implement Strongly Typed Identifiers with Type Checking in C 17?. For more information, please follow other related articles on the PHP Chinese website!