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

[temp.variadic] description of the instantiation of fold-expressions #4542

Closed
xmh0511 opened this issue Mar 8, 2021 · 6 comments · Fixed by #4543
Closed

[temp.variadic] description of the instantiation of fold-expressions #4542

xmh0511 opened this issue Mar 8, 2021 · 6 comments · Fixed by #4543
Assignees

Comments

@xmh0511
Copy link
Contributor

xmh0511 commented Mar 8, 2021

In the current draft, there are two issues about the instantiation for a fold-expression.
The first is the standard seems not clear for stating which are the elements of the pack expansion parameters that appear in the pattern of fold-expression. It could be assumed they're stated in paragraph 8 temp.variadic#8.

However, such a layout of the rule seems that the description for what these elements of pack expansion parameters are is only applying to the case that is: the instantiation of a pack expansion that is neither a sizeof... expression nor a fold-expression.

Is it necessary to take the rule out from paragraph 8 to be an individual rule? That would clearly cover the case that what these elements of the pack expansion parameters are which appears in the fold-expression.

The second issue is about the binary fold-expression when the number of the elements is zero. The whole paragraph 10 only talks about how the result is for the unary fold-expression when the number of the elements is zero. It lacks the case of a binary fold-expression. (Presumably, the result of this case is that of the expression E).

#include <iostream>
struct A{
    A* node;
};
A a{};
template<typename...T>
auto fun(T...member){
    return (&a .*....* member);
}
int main() {
    a.node = &a;
    auto r = fun();
}

Such as this example, even though the first operand &a would violate expr.mptr.oper#2

@jensmaurer
Copy link
Member

jensmaurer commented Mar 8, 2021

I think the description is clear that N=0 means the result of a binary fold is E.

For the first point, I think I agree the presentation could be improved by first introducing E_i (in general) and then explaining where those appear.

@jensmaurer jensmaurer changed the title The description of the instantiation of fold-expression in [temp.variadic] [temp.variadic] description of the instantiation of fold-expressions Mar 8, 2021
@xmh0511
Copy link
Contributor Author

xmh0511 commented Mar 9, 2021

result

For the second point, I don't think it's clear. In paragraph 10, the rule only states how the result would be for a unary fold-expression when the element of the pack expansion parameter is empty, It does not talk about binary fold-expression in this case.

Although there's a rule possibly relate to the case, which inhabits at the end of paragraph 8, that is:

Such an instantiation does not alter the syntactic interpretation of the enclosing construct, even in cases where omitting the list entirely would otherwise be ill-formed or would result in an ambiguity in the grammar.

However, at this time, I think the rule prefers to describe the case for "The instantiation of a pack expansion that is neither a sizeof... expression nor a fold-expression..." when N is zero.

Consider this example

#include <iostream>
template<class...T>
void fun(T...args){
    std::initializer_list<bool> c = {true&&args...};
    std::cout<< c.size()<<std::endl;  // its result is 0
    std::initializer_list<bool> c2 = {(true&&...&&args)};
    std::cout<< c2.size()<<std::endl; // its result is 1
}

int main() {
    fun();
}

The size of c is zero rather than one element which has the value true. It's quite different with binary fold-expression. So, I think it's necessary to state the different results between a binary fold-expression and a normal expansion when their N is zero.

@jensmaurer
Copy link
Member

I'm saying that the syntactic expansion described for binary fold-expressions in p10 does work for N=0, and thus N=0 does not need a special case in the prose wording.

@xmh0511
Copy link
Contributor Author

xmh0511 commented Mar 9, 2021

I'm saying that the syntactic expansion described for binary fold-expressions in p10 does work for N=0, and thus N=0 does not need a special case in the prose wording.

Maybe I see your meaning. Do you mean op and Ei are pair-wise?

@jensmaurer
Copy link
Member

jensmaurer commented Mar 9, 2021

Sort-of. Look at the expansion of a unary fold:

((E 1 op E 2 ) op · · · ) op E N

What's the result for N=1? It's E_1 (there is no E_2 at all).

Now look at binary fold:

(((E op E 1 ) op E 2 ) op · · · ) op E N

What's the result for N=0? With analogy to the foregoing, it's obviously E (there is no E_i at all).

@xmh0511
Copy link
Contributor Author

xmh0511 commented Mar 9, 2021

Sort-of. Look at the expansion of a unary fold:

((E 1 op E 2 ) op · · · ) op E N

What's the result for N=1? It's E_1 (there is no E_2 at all).

Now look at binary fold:

(((E op E 1 ) op E 2 ) op · · · ) op E N

What's the result for N=0? With analogy to the foregoing, it's obviously E (there is no E_i at all).

Thanks. That's my understanding of what you are saying. That is, from right to left the op and its subsequent expression are viewed as an instantiated component.

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

Successfully merging a pull request may close this issue.

2 participants