473,385 Members | 1,570 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

casting away constness

I am looking for the "cleanest" way to cast away the constness of a
pointee in C89, something like const_cast<T*>() in C++. Actually, I am
using:

#define CONST_CAST(typename,value) \
(((union { const typename cv; typename v; }*)&(value))->v)

which requires value to be an lvalue, but avoid compiler warning (gcc)
comparing to the brute force of ((typename)(value)):

extern const struct A *a_global;
struct A *a_local = CONST_CAST(struct A*,a_global); // no warning

(this macro is exclusively used to convert pointers to structures
which are known to be never defined const to a non-const ADT which is
never defined and forbid access to the components.)

The web seems to be rather silent on this topic so any hint/comment
are welcome.

a+, ld.

Sep 12 '07 #1
2 3548
Laurent Deniau <La************@gmail.comwrites:
I am looking for the "cleanest" way to cast away the constness of a
pointee in C89, something like const_cast<T*>() in C++. Actually, I am
using:

#define CONST_CAST(typename,value) \
(((union { const typename cv; typename v; }*)&(value))->v)

which requires value to be an lvalue, but avoid compiler warning (gcc)
comparing to the brute force of ((typename)(value)):
??? What's wrong with the "brute force" solution? I consider your macro
to be way less cleaner then plain casting. Following code compiles w/o
any errors:

#v+
int main(void) {
int ret = 0;
const int *cv = &ret;
int *v = (int*)cv;
return *v;
}
#v-

The web seems to be rather silent on this topic so any hint/comment
are welcome.
Maybe because you shouldn't "cast away" const qualifier?

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--
Sep 12 '07 #2
On Wed, 12 Sep 2007 06:33:40 -0700, Laurent Deniau wrote:
On 12 sep, 13:31, Michal Nazarewicz <min...@tlen.plwrote:
>Laurent Deniau <Laurent.Den...@gmail.comwrites:
I am looking for the "cleanest" way to cast away the constness of a
pointee in C89, something like const_cast<T*>() in C++. Actually, I
am using:
#define CONST_CAST(typename,value) \
(((union { const typename cv; typename v; }*)&(value))->v)
which requires value to be an lvalue, but avoid compiler warning
(gcc) comparing to the brute force of ((typename)(value)):

??? What's wrong with the "brute force" solution? I consider your
macro to be way less cleaner then plain casting.

but it warns if value is not of type typename while "brute force"
doesn't.
Not reliably. Depending on the compiler version and compiler options, you
may run into a warning about a violation of the aliasing rules. With the
compiler options I normally use, this warning is not enabled.
Additionally, you still break C's rules, including the aliasing rules.
It's just in a way that gcc doesn't warn about.
> Following code compiles w/o
any errors:

#v+
int main(void) {
int ret = 0;
const int *cv = &ret;
int *v = (int*)cv;
return *v;}

#v-

gcc emits a warning on this code (and I always use -Werror ;-):

const.c: In function 'main':
const.c:4: warning: cast discards qualifiers from pointer target type
Only if you use the -Wcast-qual option. This warning option is not
enabled by default and not included in any warning group. So you are
explicitly asking gcc to let you know when you are casting away
constness, and then you decide you would like to find a way to cast away
constness in a way that gcc can't detect because you don't like the
warnings you're getting. The proper solution is to not use that compiler
option in the first place.
Sep 12 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Martin Magnusson | last post by:
I have a typedef which looks like this: typedef std::list< const S* const > seq_type; I also have a class with a private field: seq_type m_Sequence; When calling the following method
15
by: Trevor Lango | last post by:
I want to be able to cast away the constness of a private member variable in a member function of a class. I have the private data member declared as follows: const double x; I have an...
14
by: Enrico `Trippo' Porreca | last post by:
Given: typedef struct Node Node; struct Node { void *obj; Node *next; }; typedef struct Stack Stack; struct Stack {
31
by: dragoncoder | last post by:
Consider the code class A { private: int a; }; int main(void) { A x; int* ptr = (int*)&x;
7
by: dotnetchic | last post by:
Can someone explain to me the difference between static_cast<T>(v) and (what's the term for it?) old-style C cast? UINT value1 = 0; int value2 = (int)value1; int value2 =...
9
by: Alex Vinokur | last post by:
Is this approach safe? class Foo { // Stuff }; void func1 (void *) { // Do something
5
by: brekehan | last post by:
I've always been a little sketchy on the differences between static, dynamic, and reinterpret casting. I am looking to clean up the following block by using C++ casting instead of the C style...
5
by: jason.cipriani | last post by:
There have been some recent threads about casting pointers to and from void* that have me rethinking some of my usual practices. I have a couple of questions. 1. What is the purpose of C++'s...
10
by: Alex Vinokur | last post by:
Hi, Is it possible to do C++-casting from const pair<const unsigned char*, size_t>* to const pair<unsigned char*, size_t>* ? Alex Vinokur
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.