Here's round two of my silly questions. How come this compiles:
----------------------------------
int main (int argc, char * argv[])
{
const int test1 = 5;
const int test2 = const_cast<const int &> (test1);
return 0;
}
----------------------------------
But this does not.
----------------------------------
int main (int argc, char * argv[])
{
const int test1 = 5;
const int test2 = const_cast<const int> (test1);
return 0;
}
----------------------------------
Error 1 error C2440: 'const_cast' : cannot convert from 'const int' to 'int' j:\test\main.cpp 4
----------------------------------
I definitely have the latest version of VC++ Express Edition this time.
Question about const_cast
kopo
NetPochi
Ah, but that cannot be done through const_cast, or any other cast. What you can do, however, is either
RyanB88
So you're saying that Visual C++ 2005 violates the standard, but it's ok because the other major compilers do it too
WarrenT
I'm not trying to use const_cast to cast away constness. I'm trying to use it to cause the compiler to fail if I accidentally try to assign a variable of one type to a variable of another type.
I don't like that you can do this:
const double test1 = 5.1; // 5.1
const int test2 = test1; // The integer 5
So I want to do this:
const double test1 = 5.1 // 5.1
const int test2 = const_cast<const int> (test1); // Error.
But I want it to work ok when I do this:
const int test1 = 5; // The integer 5
const int test2 = const_cast<const int> (test1); // Fine - The integer 5
I'm fine with casting to a reference, but I'm curious to know why casting to a value doesn't work just as well.
The standard says that an exp_ression may be cast to its own type using a const_cast operator. That's the functionality I was looking for.
Camillo777
Obviously it can. I'm using it for that. I'm just wondering why I have to cast it to a reference instead of to a value.
This is all well and good as long as it's just me using the code, but I don't want everyone who uses my code to have to set the compiler warnings.
Yeah. I wrote my own compile-time assert that's a template rather than a macro. But this won't work. I'm trying to write portable code and who's to say that an int isn't the same size as a double Or more importantly, I also want to prevent accidental assignments from float to int.
Regardless of any of that, I already have the functionality I need with const_cast. I just want to know why I have to cast to a reference. That's all.
UnKnown Nick
Thank you. I don't care about its "intended" use. I care about is what it does. Actually, I also want to break conversions from char to int and float to double. For my specific needs, these implicit conversions are not "perfectly good."
That's interesting because I read that whole section on const_cast and I didn't see where it said that. Please show me. Thanks.
Walter R
And it goes on. The standard does not specify that const_casts between non-reference or non-pointer types should be possible. Why It doesn't make sense for const_cast to provide this functionality.
Exploder
To begin with the conclusion: Your second example is a by-value assignment, and there's no need to use const_cast to remove the constness off such.
Consider the following
x will be a constant variable in memory, to which the compiler will refuse to assign values. The variable is read only. When you assign this to the y variable, x's value is copied into a non-constant integer; obeying the read only constraint. After executing both lines, the 10 will exist in two different memory locations.
Now consider:
This will essentially do the same: at the end of the execution, two variables will exist in memory. Compared to the first example, however, a temporary integer reference is created on line two.
It makes no sense to do it, but the compiler happily compiles it, both in expanded version as this last example shows, and the temporary variable version you provided.
Finally consider:
Will make so little sense, that the compiler simply refuses to do it. const_cast is defined and implemented to remove the constness off references and pointers, so the compile will fail if it's attempted used to provide values.
What you can do with const_cast, or rather what it's designed to do (although the example is horrible with regard to const obeyance) is:
pogmothoin
No, it's slightly more complex. It specifies what is supposed to be possible, and that anything else isn't. And let me repeat this once more: it doesn't make sense for const_cast to allow casts of non-reference or non-pointer types, so it won't. It's really that simple. You are attempting to do something which borderlines whats possible, and you will simply have to live with the consequences.
It's not my intention to sound rude here, but I am getting somewhat tired of repeating myself. If you doubt my judgement, test other major (or minor) compilers -- they'll tell you exactly what I've been trying to.
Prudhvidhar
Oh. In the draft that I have (2006-11-03), right below where it says this:
-----------------------------------------
No other conversions shall be performed explicitly using const_cast.
-----------------------------------------
It says this:
-----------------------------------------
[Note: Subject to the restrictions in this section, an exp ression may be cast to its own type using a const_cast operator. -end note]
-----------------------------------------
Is that line not in the official version
JRDodd
You don't like that you can read the variable test1, or the fact that you can initialize the variable test2 A const value is read only. Those two lines does not enable anyone to change the value that is test1, and it does not enable anyone to change the value that is test2. Both variables are at two different memory locations, and changing one does not change the other.
If you initialize the test2 variable with the vlaue from test1, you will have two const variables, one double and one int. The const_cast is redundant.
I don't think that const_cast is the real issue at hand here.. You seem to slightly misinterpret what const variables are, and perhaps also the difference between values, pointers and references. How do you think that your first example may corrupt your application, or program flow
Node_Pointer
I'm not saying that those two lines enable anyone to change the value of test1. What I'm saying is that I'm accidentally truncating the value of test1 when storing it in test2 and I want to prevent that from accidentally happening by using a const_cast.
Yes. I realize that, in this case, the const_cast is redundant, but it wouldn't be redundant if instead of casting from a double, I were casting from some typedef and I didn't know whether that typedef stood for an int or not. The compiler would then catch the error at compile time.
I just want to prevent myself from accidentally assigning doubles to ints and similar things.
AWolf
Very well, I'll stop telling you it's bad practice, unintended use, and could possibly create unoptimized code. Just remember that your approach also breaks all perfectly good conversions, such as char to int, float to double as well as all implicit conversions of class types (though constructors, assignment operators and custom conversion operators).
The error noted in your first post occurs because the standard forbids the use of const_cast with types that aren't either; pointers, references, pointer to member to an object type.
Ather.
It's there, and I've highlighted the important part. It may be cast to its own type, as long as the destination is a pointer or reference.