Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception to ES.63 for base type without any data member #2206

Open
hwisungi opened this issue Jun 13, 2024 · 3 comments
Open

Exception to ES.63 for base type without any data member #2206

hwisungi opened this issue Jun 13, 2024 · 3 comments

Comments

@hwisungi
Copy link

After fixing a false negative bug in msvc code analysis, some library code were newly tagged for violations of ES.63 rule. One of the patterns that received those warnings was brought to our attention as potential candidate of exception to the rule. Here is the pattern:

    template<typename T>
    struct Tag { /* No data member */ };

    template<typename T>
    constexpr int value(Tag<T>);

    template<typename T>
    struct S : Tag<T> {
        int x;
    };

    template<typename T>
    void f() {
        S<T> s;
        value(s);
    }

Serving as a tag type, Tag does not need any data from the source object to perform its operations. Thus, it should be more efficient for a new instance of T to be created from the source object than creating a pointer / reference to the source object.

So, it was suggested to add an exception for this pattern to rule ES.63.

@GabrielDosReis
Copy link
Contributor

Yes, there is not much cause for slicing concerns in these scenarios of tag-based selection.

Suggestion: warn only if both the base class and the derive class have non-static data members.

@prathameshatkare
Copy link

suggested to consider an exception to this rule for tag types because:

Tag Types (like Tag) don't have data members: They serve purely as type markers and carry no data. Hence, copying or passing them by value is neither expensive nor inefficient.

Copying is lightweight: Since Tag (and other similar tag types) don't contain any member data, the cost of copying them is essentially zero. There's no advantage in using a pointer or reference over passing by value in this case.

Semantics and design: Tag types are often used as simple identifiers for types or traits, and it's semantically more straightforward to pass them by value. Creating pointers or references to tag types introduces unnecessary complexity without any benefit in performance.

@BjarneStroustrup
Copy link
Contributor

In the original example, what useful action could value() perform? Anything more than "not implemented"?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants