Skip to content

Commit

Permalink
C++23 multidimensional subscript operator
Browse files Browse the repository at this point in the history
  • Loading branch information
guwirth committed Aug 29, 2024
1 parent 696d8f3 commit 993f5b6
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 31 deletions.
6 changes: 2 additions & 4 deletions cxx-squid/dox/2023-03-19_cpp23_grammar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -436,8 +436,7 @@ nested-name-specifier:

lambda-expression:
lambda-introducer attribute-specifier-seqopt lambda-declarator compound-statement
lambda-introducer < template-parameter-list > requires-clauseopt attribute-specifier-seqopt
lambda-declarator compound-statement
lambda-introducer < template-parameter-list > requires-clauseopt attribute-specifier-seqopt lambda-declarator compound-statement

lambda-introducer:
[ lambda-captureopt ]
Expand All @@ -446,8 +445,7 @@ lambda-declarator:
lambda-specifier-seq noexcept-specifieropt attribute-specifier-seqopt trailing-return-typeopt
noexcept-specifier attribute-specifier-seqopt trailing-return-typeopt
trailing-return-typeopt
( parameter-declaration-clause ) lambda-specifier-seqopt noexcept-specifieropt attribute-specifier-seqopt
trailing-return-typeopt requires-clauseopt
( parameter-declaration-clause ) lambda-specifier-seqopt noexcept-specifieropt attribute-specifier-seqopt trailing-return-typeopt requires-clauseopt

lambda-specifier:
consteval
Expand Down
28 changes: 5 additions & 23 deletions cxx-squid/dox/diff-cpp20-cpp23_grammar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,16 @@ d-char:

lambda-expression:
lambda-introducer attribute-specifier-seqopt lambda-declarator compound-statement
lambda-introducer < template-parameter-list > requires-clauseopt attribute-specifier-seqopt
lambda-declarator compound-statement
lambda-introducer < template-parameter-list > requires-clauseopt attribute-specifier-seqopt lambda-declarator compound-statement

lambda-introducer:
[ lambda-captureopt ]

lambda-declarator:
lambda-specifier-seq noexcept-specifieropt attribute-specifier-seqopt trailing-return-typeopt
noexcept-specifier attribute-specifier-seqopt trailing-return-typeopt
trailing-return-typeopt
( parameter-declaration-clause ) lambda-specifier-seqopt noexcept-specifieropt attribute-specifier-seqopt
trailing-return-typeopt requires-clauseopt
( parameter-declaration-clause ) lambda-specifier-seqopt noexcept-specifieropt attribute-specifier-seqopt trailing-return-typeopt requires-clauseopt

lambda-specifier:
consteval
Expand All @@ -163,25 +164,6 @@ requirement-seq:
requirement
requirement requirement-seq

postfix-expression:
primary-expression
postfix-expression [ expression-listopt ]
postfix-expression ( expression-listopt )
simple-type-specifier ( expression-listopt )
typename-specifier ( expression-listopt )
simple-type-specifier braced-init-list
typename-specifier braced-init-list
postfix-expression . templateopt id-expression
postfix-expression -> templateopt id-expression
postfix-expression ++
postfix-expression --
dynamic_cast < type-id > ( expression )
static_cast < type-id > ( expression )
reinterpret_cast < type-id > ( expression )
const_cast < type-id > ( expression )
typeid ( expression )
typeid ( type-id )

**A.6 Statements [gram.stmt]**

init-statement:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ private static void expressions(LexerfulGrammarBuilder b) {
),
b.zeroOrMore(
b.firstOf(
b.sequence("[", exprOrBracedInitList, "]"), // C++
b.sequence("[", b.optional(expressionList), "]"), // C++23
b.sequence("(", b.optional(expressionList), ")"), // C++
b.sequence(
b.firstOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,6 @@ void postfixExpression() {
mockRule(CxxGrammarImpl.expression);
mockRule(CxxGrammarImpl.idExpression);
mockRule(CxxGrammarImpl.typeId);
mockRule(CxxGrammarImpl.exprOrBracedInitList);
mockRule(CxxGrammarImpl.cudaKernel);

assertThatParser()
Expand All @@ -313,7 +312,8 @@ void postfixExpression() {
.matches("const_cast < typeId > ( expression )")
.matches("typeid ( expression )")
.matches("typeid ( typeId )")
.matches("primaryExpression [ exprOrBracedInitList ]")
.matches("primaryExpression [ ]") // C++23
.matches("primaryExpression [ expressionList ]") // C++23
.matches("primaryExpression ( )")
.matches("primaryExpression ( expressionList )")
.matches("primaryExpression . idExpression")
Expand Down Expand Up @@ -342,7 +342,9 @@ void postfixExpression_reallife() {
.matches("G::typeid")
.matches("int::typeid")
// CUDA
.matches("kernel<<<gridDim,blockDim,0>>>(d_data, height, width)");
.matches("kernel<<<gridDim,blockDim,0>>>(d_data, height, width)")
// C++23
.matches("array[a, b]");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
template<typename T, std::size_t X, std::size_t Y>
struct Matrix {

static inline std::array<T, X * Y> mat{}; // (2)

static T& operator[](std::size_t x, std::size_t y) { // (1)
return mat[y * X + x];
}
};

int main() {

std::cout << '\n';

Matrix<int, 3, 3> mat;
for (auto i : {0, 1, 2}) {
for (auto j : {0, 1, 2}) mat[i, j] = (i * 3) + j;
}
for (auto i : {0, 1, 2}) {
for (auto j : {0, 1, 2}) std::cout << mat[i, j] << " ";
}

std::cout << '\n';

}

0 comments on commit 993f5b6

Please sign in to comment.