I have a template class like this:
template<T>
class MyClass
{
T* data;
}
Sometimes, I want to use the class with a constant type T as follows:
MyClass<const MyObject> mci;
but I want to modify the data using const_cast<MyObject*>data
(it is not important why but MyClass
is a reference count smart pointer class which keeps the reference count in the data itself. MyObject
is derived from some type which contains the count.
The data should not be modified but the count must be modified by the smart pointer.).
Is there a way to remove const-ness from T
? Fictional code:
const_cast<unconst T>(data)
?
const_cast is one of the type casting operators. It is used to change the constant value of any object or we can say it is used to remove the constant nature of any object. const_cast can be used in programs that have any object with some constant value which need to be changed occasionally at some point.
As a common rule, it is very often considered a bad practice to use const_cast<>() in C++ code as it reveals (most of the time) a flaw in the design.
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) .
First create a constant variable of type int and give it some suitable size, let's say “a” and its value be 20. Then create a constant pointer, let us say “b” of the same data type and allocate it the address of our constant variable “a”.
The simplest way here would be to make the reference count mutable.
However, if you are interested in how it would work with the const_cast
, then reimplementing boost's remove_const
should be quite simple:
template <class T>
struct RemoveConst
{
typedef T type;
};
template <class T>
struct RemoveConst<const T>
{
typedef T type;
};
const_cast<typename RemoveConst<T>::type*>(t)->inc();
You have the answer. const_cast works in both directions:
char* a;
const char* b;
a = const_cast<char*>(b);
b = const_cast<const char*>(a); // not strictly necessarily, just here for illustration
As for you specific issue, have you considered the mutable keyword? It allows a member variable to be modified inside a const method.
class foo {
mutable int x;
public:
inc_when_const() const { ++x; }
dec_when_const() const { --x; }
};
Make the reference count mutable in the class managed by your intrusive pointer. This is entirely reasonable, and reflects "logical constness" exactly correctly -- i.e. changing the object's reference count does not reflect any change in the state of the object itself. In other words, the reference count isn't logically part of the object -- the object just happens to be a convenient place to store this semi-unrelated data.
If you can use Boost, the Type Traits library provides the remove_const metafunction that does that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With