Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Dependent Expression and ODR-use in a Generic Lambda in C++14

17 views
Skip to first unread message

jose...@solidsands.nl

unread,
Mar 3, 2017, 9:06:26 AM3/3/17
to

This is an example from the C++14 Standard (ISO/IEC 14882:2014), Section 5.1.2, Paragraph 12. which I modified in 2 ways:

- First, both functions f have "int" as the first argument, so variable x is NOT odr-used in any case.
- Second, I removed (commented) the capture-default in the lambda g2.

Is this code Standard-compliant? Both clang and gcc compile successfully. However, in the original example lambda g2 had a capture default, so variable x was implicitly captured because there was a dependent expression (and it could be also odr-used). Note that in the aforementioned paragraph of the Standard there are 2 conditions to implicitly capture variable x (odr-use OR dependent expression). Now, without capture-default nor odr-use:

- Shouldn't this be a compile-time error (assume g2 calls with both kinds of arguments) since there is a dependent expression and no capture-default? (so variable x needs to be captured but it cannot be)
- Or does the condition of the dependent expression to implicitly capture a variable only apply when there is a capture-default? That would mean that, with no odr-use (i.e., without const int&) the program will work in the same way regardless of the capture-default. Therefore, wouldn't the second condition of the dependent expression be useless?

void f( int, const int (&)[2] = {}) { } // #1

void f( int, const int (&)[1]) { } // #2

// void f(const int&, const int (&)[1]) { } // #2_original

void test() {

const int x = 17;

auto g = [](auto a) {

f(x); // OK: calls #1, does not capture x

};

auto g2 = [ /* = */ ](auto a) {

int selector[ sizeof(a) == 1 ? 1 : 2 ]{};

f(x, selector); // OK: is a dependent expression, so captures x ???

};

}
0 new messages