The library is "wrong" with a pass by reference, and it should not be compiled, why did it compile?

I used yaml-cpp, the yaml syntax library, and I went crazy because my yaml document was not completely parsed. It turns out because the constructor should have given a link, not an object.

wrong code:

ifstr; YAML::Parser parser(ifstream("items9.yml")); 

correct code:

 ifstream ifstr("items9.yml"); YAML::Parser parser(ifstr); 

The man told me that he should not compile, I use visual C ++ 10. Is this normal behavior and should I know about it, is the library incorrectly designed or visually C ++ mistakenly accepts the code?

+4
source share
1 answer

This is a known issue in VS, which (unlike the standard) allows non-constant links to rvalues ​​to be linked. The same thing can be verified with this code:

 struct test {}; test f() { return test(); } int main() { test & r = f(); // Should be an error } 
+2
source

Source: https://habr.com/ru/post/1403528/


All Articles