Home > Backend Development > C++ > How Can I Implement Strongly Typed Identifiers with Type Checking in C 17?

How Can I Implement Strongly Typed Identifiers with Type Checking in C 17?

Linda Hamilton
Release: 2024-11-16 08:23:02
Original
1065 people have browsed it

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...
}
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template