Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is (int&)0 ill-formed?

According to [expr.cast]/4, a C-style cast tries the following casts in order:

  1. const_cast
  2. static_cast
  3. static_cast followed by const_cast
  4. reinterpret_cast
  5. reinterpret_cast followed by const_cast

The following cast is well-formed:

const_cast<int&>(static_cast<const int&>(0))

Yet both GCC and Clang reject the cast (int&)0. Why?

like image 776
Brian Bi Avatar asked Mar 30 '18 21:03

Brian Bi


People also ask

Why do we use int?

int main means that the main function returns an integer value.so in case of integer, we use int in C programming. Int keyword is used to specify integer datatype . It's size may be 16,32,64 bits depending on the machine or further short /long types. int is a datatype which is used to represent integer values.

Why is C Main an int?

The short answer, is because the C++ standard requires main() to return int . As you probably know, the return value from the main() function is used by the runtime library as the exit code for the process. Both Unix and Win32 support the concept of a (small) integer returned from a process after it has finished.

How do you use int?

The Excel INT function returns the integer part of a decimal number by rounding down to the integer. Note that negative numbers become more negative. For example, while INT(10.8) returns 10, INT(-10.8) returns -11. number - The number from which you want an integer.

What is a int in C?

int. Integers are whole numbers that can have both zero, positive and negative values but no decimal values. For example, 0 , -5 , 10. We can use int for declaring an integer variable.


1 Answers

It is a bug in gcc and clang.

According to CWG 909, the "straightforward interpretation of the wording" should be used when generating cast sequences for C-style casts; there, the first cast invokes a conversion operator, but there is no reason to think that it should be any different when the first cast is reference binding a temporary.

I've posted a patch to the gcc mailing list, generated from this git branch, but it hasn't received any attention so far.

like image 126
ecatmur Avatar answered Oct 18 '22 04:10

ecatmur