Reputation: 4274
I usually use C type casting in C/C++ code. My question is, does adding the "const" keyword in the casting type mean anything to the result?
For example, I can think up several scenarios:
const my_struct *func1()
{
my_struct *my_ptr = new my_struct;
// modify member variables
return (const my_struct *)my_ptr;
// return my_instance;
}
In this one, the function constructs a new instance of a struct, and casting it to to a constant pointer, therefore caller will not be able to further modify its internal state except deleting it. Is the "const" casting required, recommended, or simply unnecessary, since either return statement works.
In this one, my_base
is the base class of my_derive
.
const my_base *func2(const my_derive *my_ptr)
{
return (const my_base *)my_ptr;
// return (my_base *)my_ptr;
}
Since my_ptr
is already a const pointer, would casting it with (my_base *)
involve a const_cast for removing const and another implicit const_cast when returning?
Is there any reason to add "const" to an integer function argument, since changing it never affect state outside the function?
void func3(const int i)
{
// i = 0; is not allowed, but why, as it is harmless?
}
How about adding "const" when casting an integer? I think this should resemble func2()
.
void func4(short i)
{
const unsigned int j = (const unsigned int) i;
// const unsigned int j = (unsigned int) i;
}
Correct me if I'm wrong. Considering type casting might be an FAQ, I'm not sure if this one duplicates with anything else. Thanks!
Upvotes: 7
Views: 42121
Reputation: 68571
Adding const
to a cast is just like adding const
to any other type specifier --- the resulting object is const
. what this means depends on the context. If you add const
at the top level (e.g. const int
or const foo
or int* const
, then you just have a const
object. In most cases this can be copied and assigned just like a non-const
object can (though there's a few exceptions, like std::auto_ptr
).
If you add the const
to a pointer then you can also add it to the pointed-to type. e.g. int * const
is a const
pointer to a plain int
, whereas const int*
or int const*
is a plain pointer to a const int
.
int i;
int* p = (int* const) &i; // fine; top-level const not a problem
int* p2 = (int const*) &i; // error; pointed-to object is const, so this is preserved
int const* p3= (int const*) &i; // OK
const
at the top level in a parameter declaration is just like declaring any other local variable const
--- you cannot modify the named object within the function body. However, this const
does not form part of the function type, so you can forward-declare the function without it (with conforming compilers at least).
void func(int i); // forward-declare func
void func(const int i) // this matches the declaration
{
// i is const here
}
This could be argued to be good style --- whether or not the variable is treated as const
inside the function is an implementation detail so shouldn't be in the prototype. Within the definition, it follows the guideline of declaring something const
if you don't intend to modify it.
Using const
elsewhere in the parameter declaration (e.g. as part of a pointer) does affect the function type, as with the casts.
Upvotes: 6
Reputation: 99565
Adding the const
keyword in the casting type means that the result will be constant. The following will not compile in C++ (in C it has no effect):
int* x = (const int*)malloc(10); // cannot convert from 'const int *' to 'int *'
You really shouldn't use C type casting in your C++ code. It is not safe and should be used only for compatibility with the legacy C code. You should use C++ casts instead.
In cases as in func3
usually const
qualifier is not used. There's no big reason to add const
qualifier to function argument if it has not pointer or not reference type. Consider the following:
void func3( TYPE i); // no reason to use `const`
void func3(const TYPE& i); // use `const`, so as not to accidentally change `i`
When you assign lvalue to rvalue, as in func4
, there's no need to explicitly specify the const
qualifier in the cast expression. Lvalue-to-rvalue conversion will be performed implicitly according to the C++ Standard 4.1.
Upvotes: 9
Reputation: 31579
In the first example returning non-const in a function that requires const will cat it again meaning that const matters, but you can't notice it because if it's not there it will be added.
In the second example it will be one cast maintaining const.
In third example it is disallowed because it is harmless: i will change for the rest of the function and unrecoverable.
Keep in mind that putting type into container not of it's type will cast it automaticllay same as int put in long field won't remain int.
You also have to remember that const means about the same as private or public to the assembler, which is nothing. The assembler code generated will not differ at all no matter where you put const and will run at same speed exacly.
Upvotes: 1