오류!int에서 double로 변환하는 이유는 무엇입니까?
$ cat double.cc
#include<iostream>
int main() {
int i = 42;
double &r = i;
}
$ g++ double.cc
double.cc: In function ‘int main()’:
double.cc:5: error: invalid initialization of reference of type ‘double&’
from expression of type ‘int’
$
작동!
$ cat double.cc
#include<iostream>
int main() {
int i = 42;
const double &r = i;
}
$ g++ double.cc
$
왜 첫 번째 경우에 오류가 발생합니까? 나는 이것이 here에 대해 논의 된 것을 알고 있지만, 왜 이것이 허용되지 않는지 이해할 수 없다. int
에서 const double&
까지 허용된다. 여기
왜 코멘트가없는 임의의 드라이브가 downvote입니까? 이것은 좋은 질문입니다. 특히 Moeb이 조사한 이후에 (적어도 Moeb에게는) 만족스럽지 않은 관련 질문을 발견했습니다. –