Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

[SUGGESTION] Down with typename! #531

Closed

Description

First expressed at #529 (comment) (which occurred to me when working on that PR).

Will your feature suggestion eliminate X% of security vulnerabilities of a given kind in current C++ code? 👌 (zero).

Will your feature suggestion automate or eliminate X% of current C++ guidance literature?

If yes, please be specific about what current good guidance this helps make the default, and/or what guidelines we would no longer need to teach/learn or that would be simplified and how,

  • T.42 would be simplified by reducing the list of cases to which the enforcement can possibly apply.

with an example or two (especially a link to a real "Effective C++" or "C++ Core Guidelines" guideline or two).

Assuming that Cpp2 supports typename for disambiguation, just like Cpp1.

-x: typename T::type = ();
+x: T::type = ();
-x is typename T::type
+x is T::type
  • Please limit suggestions to quantifiable improvements to C++ simplicity, safety, or toolability. Quantifiable means that there is some kind of measurable data that helps motivate the change and measure success. Two of the big ones that will get my attention are eliminating vulnerabilities and eliminating guidance, so use those below please.

For a measure,
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p2150r0.html#5.0
proposes to remove 254 uses of typename from the C++ standard library wording
(extracted from the linked section's HTML by counting <del>typename</del>s).

There's 765 occurrences of 'typename ' in the C++ standard library,
not all disambiguating typenames
(extracted from the library sources).
That's a 33% reduction.

Describe alternatives you've considered.
None.
There's nothing better than saying nothing at all when there's no room for another interpretation.

Activity

  1. hsutter commented on Jul 1, 2023

    @hsutter
    Owner

    First: I love the title just as much now as when I first saw Nina and Daveed use it :)

    Can you elaborate on what the proposal is though? Cpp2 currently doesn't use typename, and if I discover any places where it would be needed my first thought would be to fix the grammar so it's unambiguous.

    Assuming that Cpp2 supports typename for disambiguation, just like Cpp1.

    -x: typename T::type = ();
    +x: T::type = ();

    The first line should not be allowed now, and the second is unambiguously a variable.

    -x is typename T::type
    +x is T::type

    is is interesting:

    1. Dependent types: That x is T::type should work but doesn't yet, thanks for the bug report. It ought to emit typename for a dependent type... in this case it should generate cpp2::is<typename T::type>(x). But currently it emits cpp2::is<T::type>(x) which fails the Cpp1 compiler. I'll need to fix that.

    2. Dependent (static) values:

    a) Currently x is (T::value) works, and requires ( ) which makes it unambiguous.

    b) Note that to test this you have to write the type in Cpp1, because I haven't yet provided a way to author static values in Cpp2. I probably will allow them, but require them to be constinit or constexpr.

    Here's a working test program:

    struct Test {
        using type = int;
        static inline int value = 42;
    };
    
    dependent_is: <T> () = {
        x := 42;
        std::cout
            // (x is T::type) << "\n" // doesn't work yet, need to inject `typename`
            << (x is (T::value)) << "\n";
    }
    
    main: () = {
        x := 42;
        std::cout << std::boolalpha
            << (x is int) << "\n" // "true"
            << (x is 42) << "\n"; // "true"
        dependent_is<Test>();     // "true"
    }
    
  2. hsutter commented on Jul 5, 2023

    @hsutter
    Owner

    Would you be solving the ambiguity in template-argument the same as with is-as-expression?

    Ack: You beat me to it -- I've been out of computer range but was going to reply saying I expected it would be the same as x is type and x is (value). The parens aren't needed for things like literals, of course.

    I hope to finally be back in reliable computer range again on Friday...

  3. Repository owner locked and limited conversation to collaborators on Aug 30, 2023
  4. converted this issue into a discussion #628 on Aug 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions