auto
C++ 11 이상 유형을 어떻게 해석하는지 오해해야합니다. int
(int a = ir/L;
이) 잘 컴파일로 auto
교체, 그러나C + + 11 int & and long에서 산술 연산에서 자동 유형 공제
test.cpp: In function 'int main()':
test.cpp:12:10: error: invalid initialization of non-const reference
of type ‘int&’ from an rvalue of type ‘int’
foo(a);
^
test.cpp:1:6: note: initializing argument 1 of ‘void foo(int&)’
void foo(int& x)
^~~
및 foo()
호출하기 전에 예상 된 결과 (a == 0
을 제공합니다
void foo(int& x)
{
++x;
}
int main()
{
int i = 2;
int& ir = i;
long L = 5;
auto a = ir/L;
foo(a);
return 0;
}
이 컴파일러 오류가 발생합니다 : 나는 다음과 같은 코드가 있습니다 , 및 이후 a == 1
). 코드를 둘러보고 다양한 오류 메시지를보고 난 후에는 auto
이 long int&
으로 추론됩니다. void bar(int x)
및 void bar(const int& x)
함수를 정의하면 call of overloaded ‘bar(long int&)’ is ambiguous
이라는 오류 메시지가 나타납니다. 댓글에서
보정 :
이해가 어떻게 const가 아닌 심판에 의해 전달 될 수 좌변에서 auto x = [int&]/[int]
결과 동안에는 반지를 rvalue에 auto x = [int&]/[long]
결과가 없습니다.
'a'는 참조가 아니며 간단한 'long'입니다. – LogicStuff
^^^^^^ [이젠 길다] (http://ideone.com/lGpcWt) – StoryTeller
이유가 오래 걸리는 이유를 모르면 유형 판촉이 그 원인입니다. 그리고 당신은 [이것에 대한 답을 읽어야합니다] (http://stackoverflow.com/questions/6770258/how-do-promotion-rules-work-when-the-signness-on-either-side-of-a-binary -opera) – StoryTeller