Back to questions
JuniorTheoryCommon
Which line does not compile?
#include <iostream>
int main() {
int a = 2.9;
int b(2.9);
double c{2};
int d{2.9};
std::cout << a << b << c << d << '\n';
}int d{2.9};: braces refuse to drop the fraction of 2.9
- Expecting
=to refuse narrowing:int a = 2.9;compiles and drops the fraction, soais2; only braces make narrowing an error. - Expecting parentheses to work like braces:
int b(2.9);converts the value as=does and compiles; only{}checks for narrowing.
- Why does
double c{2};compile, while the clang compiler rejectsint n = 2; double r{n};? - How would you rewrite
int d{2.9};so that it compiles when the fraction should be dropped?