tags:

views:

1229

answers:

3

Why is the following?:

  const int i0 = 5;
//int       i1 = const_cast<int>(i0);       // compilation error
  int       i2 = (int)i0;                   // okay

  int       i3 = 5;
//const int i4 = const_cast<const int>(i3); // compilation error
  const int i5 = (const int)i3;             // okay
A: 

For the first error. const_cast can only be used on pointer or reference types. "int" is neither. This may or may not be the C++ standard (couldn't find a good reference). But it is the case for certain implementations such as MS's C++ compiler.

For the second error. const_cast can only be used to remove a const or volatile qualifier not add it.

Reference: http://msdn.microsoft.com/en-us/library/bz6at95h(VS.80).aspx

JaredPar
The const_cast operator (C++ only)A const_cast operator is used to add or remove a const or volatile modifier to or from a type. From: http://publib.boulder.ibm.com/infocenter/compbgpl/v9v111/index.jsp?topic=/com.ibm.xlcpp9.bg.doc/language_ref/keyword_const_cast.htm
+10  A: 
  const int i0 = 5;
//int       i1 = const_cast<int>(i0);       // compilation error
  int       i2 = (int)i0;                   // okay

  int       i3 = 5;
//const int i4 = const_cast<const int>(i3); // compilation error
  const int i5 = (const int)i3;             // okay

The compilation errors are caused because you don't cast const away/add const. Instead, you copy i0. For that operation, no cast is required at all:

int i1 = i0;
const int i4 = i3;

The type you cast to should actually be a pointer or reference. Otherwise, using const_cast doesn't make sense since you could straight copy it. For pointers, for example, you can cast away the const, because dereferencing the pointer will yield another type for a const T* (yields const T) than for a T* (yields T). For references, the same is true: T& will access the object using another this pointer type than using const T&. Now what you really wanted to archive:

  const int i0 = 5;
//int &     i1 = const_cast<int&>(i0);      // okay (but dangerous)
  int &     i2 = (int&)i0;                  // okay (but dangerous)

  int       i3 = 5;
//const int&i4 = const_cast<const int&>(i3); // ok now and valid!
  const int&i5 = (const int&)i3;             // okay too!

The above can lead to undefined behavior, when you write to an originally const object through a reference to non-const (actually, merely casting and reading it isn't undefined behavior in itself. But if you're casting away const, you may also write to it, which then yields the undefined behavior)

Johannes Schaub - litb
Is it really undefined behavior to simply HAVE a non-const reference to a const object? I thought you actually had to attempt to modify it. You example doesn't access i2 at all; it just defines it.
Rob Kennedy
Are there any situations where a C-style cast from (int) to (const int) or from (const int) to (int) would be useful, being that const_cast<int> and const_cast<const int> are not allowed?
kenny, no there are no situations. there are no constant rvalues of builtin type. so those would either be illegal, or just the same as "int" (depends on what the standard says, haven't looked up im in school currently)
Johannes Schaub - litb
+4  A: 

It's an error because the standard says it's not allowed. The standard enumerates the kinds of conversions that const_cast is allowed to do, and it disallows anything not on the list. It allows the following:

  • Pointers
  • References
  • Member-pointers

Since your types aren't any of those, they're not allowed.

On the bright side, the examples you gave don't need const_cast, either.

Rob Kennedy