[SUGGESTION] Down with typename
!
#628
Replies: 11 comments 5 replies
-
Note that because the Cpp2 grammar is unambiguous For example,
|
Beta Was this translation helpful? Give feedback.
-
In Cpp2, the only grammar that could be an expression or type-id is a template-argument. Now, the C++ standard library isn't such a good example because it's not actual C++ code. |
Beta Was this translation helpful? Give feedback.
-
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
The first line should not be allowed now, and the second is unambiguously a variable.
a) Currently 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 Here's a working test program:
|
Beta Was this translation helpful? Give feedback.
-
For this to work:
All those I'm actually working on a solution. #define CPP2_USE_MODULES Yes
//=== Cpp2 type declarations ====================================================
#include "cpp2util.h"
#line 1 "pure2-optional-typename.cpp2"
class t;
//=== Cpp2 type definitions and function declarations ===========================
#line 1 "pure2-optional-typename.cpp2"
class t {
public: using type = cpp2::i32;
public: t() = default;
public: t(t const&) = delete; /* No 'that' constructor, suppress copy */
public: auto operator=(t const&) -> void = delete;
#line 3 "pure2-optional-typename.cpp2"
};
template<typename T, typename T::type V> [[nodiscard]] auto f(cpp2::in<typename T::type> x) -> typename T::type;
[[nodiscard]] auto main() -> int;
//=== Cpp2 function definitions =================================================
#line 4 "pure2-optional-typename.cpp2"
template<typename T, typename T::type V> [[nodiscard]] auto f(cpp2::in<typename T::type> x) -> typename T::type { return typename T::type{x}; }
[[nodiscard]] auto main() -> int { return f<t,0>(0); }
|
Beta Was this translation helpful? Give feedback.
This comment has been hidden.
This comment has been hidden.
-
There is a grammar where it's needed: template-argument. |
Beta Was this translation helpful? Give feedback.
-
Would you be solving the ambiguity in template-argument the same as with is-as-expression? I think that would make sense, and be consistent.
If done, there might be one difference between template-argument and is-as-expression.
|
Beta Was this translation helpful? Give feedback.
-
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 I hope to finally be back in reliable computer range again on Friday... |
Beta Was this translation helpful? Give feedback.
-
The same is true for an expression template argument that is lowered without the parentheses. However, that means that changing template-argument to consider type-id before expression |
Beta Was this translation helpful? Give feedback.
-
Note that although #533 marks this PR as to-be-fixed by merging it, it doesn't implement this idea. |
Beta Was this translation helpful? Give feedback.
-
The grammar has this: //G prefix-expression:
//GTODO 'sizeof' '(' type-id ')'
//GTODO 'alignof' '(' type-id ')' It's missing |
Beta Was this translation helpful? Give feedback.
-
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?
Assuming that Cpp2 supports
typename
for disambiguation, just like Cpp1.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
typename
s(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.
Beta Was this translation helpful? Give feedback.
All reactions