Похожие чаты

Hi there, Why is this valid ? void test(int & ref); test(10);

11 ответов

19 просмотров

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

John- Автор вопроса
John- Автор вопроса
Danya🔥
It's not

Mine, It is valid.

John
MSVC ?

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

John
Me gcc.

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

John- Автор вопроса
olli
Doesn't matter, still not valid GCC reports <sourc...

Wrong posting so sorry. Should be test(const int & i); test(10); Dont you think reference should be lvalue ?

John
Wrong posting so sorry. Should be test(const int &...

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&)

John- Автор вопроса
olli
yes, that's fine. Why does it need to be an lvalue...

I want to differ operation for constant and object (lvalue). Quite dont understand about prvalue and xvalue. Let me look at it. Anyway, Thanks.

John
I want to differ operation for constant and object...

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

Похожие вопросы

Обсуждают сегодня

Карта сайта