diff --git a/sketch.cpp b/sketch.cpp index 72c9ed7..190132a 100644 --- a/sketch.cpp +++ b/sketch.cpp @@ -1412,6 +1412,55 @@ void call_expr () std::cout << "\n"; } +namespace reference_returning { + + struct number + { + double value; + }; + + number const the_result{13.0}; + + // User-defined binary-plus! With weird reference semantics! + template + inline number const & eval_plus (A a, B b) + { return the_result; } + +} + +void reference_returns () +{ + term unity = {{1.0}}; + auto expr = unity + reference_returning::number{1.0}; + + // TODO: This has to fail due to the general implicit declaration rules + // (see [conv] 3-6). This needs to be noted in docs. +#if 0 + { + reference_returning::number const & n = expr; + if (&n == &reference_returning::the_result) + std::cout << "Got the_result.\n"; + else + std::cout << "FAIL\n"; + } +#endif + + { + reference_returning::number const & n = evaluate(expr); + if (&n == &reference_returning::the_result) + std::cout << "Got the_result.\n"; + else + std::cout << "FAIL\n"; + } + + static_assert( + std::is_same_v< + decltype(evaluate(expr)), + reference_returning::number const & + > + ); +} + int main () { term_plus_x(); @@ -1432,6 +1481,8 @@ int main () call_expr(); + reference_returns(); + #if 0 // TODO { bp17::terminal unity{1.0};