Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C type casting with "const" keyword

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!

like image 600
Crend King Avatar asked Aug 06 '10 06:08

Crend King


People also ask

Can you cast something as const in C?

Const casts are only available in C++. Const casts are used to strip the const-ness or volatile-ness from a variable.

How do you cast off const?

The statement int* c = const_cast<int>(b) returns a pointer c that refers to a without the const qualification of a . This process of using const_cast to remove the const qualification of an object is called casting away constness. Consequently the compiler does allow the function call f(c) .

Does C have the const keyword?

Yes, there's a const keyword in C. It's been there since C90. Syntactically, it can occur in the same places as in C++.

Is Const cast Safe?

If you cast away the constness of an object that has been explicitly declared as const, and attempt to modify it, the results are undefined. However, if you cast away the constness of an object that has not been explicitly declared as const, you can modify it safely.


1 Answers

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.

like image 63
Kirill V. Lyadvinsky Avatar answered Oct 08 '22 17:10

Kirill V. Lyadvinsky