it is not.. void test(int&){} void foo() { test(10); } clang outputs: <source>:3:5: error: no matching function for call to 'test' test(10); ^~~~ <source>:1:6: note: candidate function not viable: expects an lvalue for 1st argument void test(int&){} ^
It's not
Mine, It is valid.
MSVC (19.27) reports <source>(3): error C2664: 'void test(int &)': cannot convert argument 1 from 'int' to 'int &' <source>(1): note: see declaration of 'test' Compiler returned: 2
Doesn't matter, still not valid GCC reports <source>: In function 'void foo()': <source>:3:10: error: cannot bind non-const lvalue reference of type 'int&' to an rvalue of type 'int' 3 | test(10); | ^~ <source>:1:11: note: initializing argument 1 of 'void test(int&)' 1 | void test(int&){} | ^~~~ Compiler returned: 1 For completeness sake, ICC reports <source>(3): error: initial value of reference to non-const must be an lvalue test(10); ^ compilation aborted for <source> (code 2) Compiler returned: 2
Wrong posting so sorry. Should be test(const int & i); test(10); Dont you think reference should be lvalue ?
yes, that's fine. Why does it need to be an lvalue? Using a prvalue is fine and a const reference can exist to an xvalue. Another example of the same principle is passing a string literal to foo(const std::string&)
I want to differ operation for constant and object (lvalue). Quite dont understand about prvalue and xvalue. Let me look at it. Anyway, Thanks.
Why do you want that? Why do you want a constant not to be considered a const reference? Otherwise you could look at rvalue references
Обсуждают сегодня