narrowing

How does narrowing work in method invocation in Java?

浪子不回头ぞ 提交于 2019-11-27 06:19:18
问题 Why this is giving the compile time error? 2 is constant at compile time, therefore narrowing should be allowed here since 2 is in range of byte. public class Test { public static void main(String[] args) { ForTest test=new ForTest(); test.sum(1, 2); //compile time error here } } class ForTest { public int sum(int a,byte b) { System.out.println("method byte"); return a+b; } } The error is: The method sum(int,byte) in the type ForTest is not applicable for the arguements (int,int). Edit: I

Narrowing conversion to bool in list-initialization - strange behaviour

风格不统一 提交于 2019-11-27 01:32:02
问题 Consider this piece of C++11 code: #include <iostream> struct X { X(bool arg) { std::cout << arg << '\n'; } }; int main() { double d = 7.0; X x{d}; } There's a narrowing conversion from a double to a bool in the initialization of x . According to my understanding of the standard, this is ill-formed code and we should see some diagnostic. Visual C++ 2013 issues an error: error C2398: Element '1': conversion from 'double' to 'bool' requires a narrowing conversion However, both Clang 3.5.0 and

Why doesn't narrowing conversion used with curly-brace-delimited initializer cause an error?

北城余情 提交于 2019-11-26 19:11:14
I learnt about curly-brace-delimited initializer in The C++ Programming Language, 4th ed. > Chapter 2: A Tour of C++: The Basics. I am quoting from the book below. The = form is traditional and dates back to C, but if in doubt, use the general {} -list form (§6.3.5.2). If nothing else, it saves you from conversions that lose information (narrowing conversions; §10.5): int i1 = 7.2; // i1 becomes 7 int i2 {7.2}; // error : floating-point to integer conversion int i3 = {7.2}; // error : floating-point to integer conversion (the = is redundant) However, I am unable to reproduce these results. I

Why doesn&#39;t narrowing conversion used with curly-brace-delimited initializer cause an error?

柔情痞子 提交于 2019-11-26 06:49:44
问题 I learnt about curly-brace-delimited initializer in The C++ Programming Language, 4th ed. > Chapter 2: A Tour of C++: The Basics. I am quoting from the book below. The = form is traditional and dates back to C, but if in doubt, use the general {} -list form (§6.3.5.2). If nothing else, it saves you from conversions that lose information (narrowing conversions; §10.5): int i1 = 7.2; // i1 becomes 7 int i2 {7.2}; // error : floating-point to integer conversion int i3 = {7.2}; // error :