Link to home
Start Free TrialLog in
Avatar of mrwad99
mrwad99Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Template code "const stripper": example usage please!

Ah hello.

I have the following code from a book I am studying.

It is supposed to strip off the constness of a type, and determine if a type is const or not.

Put simply, can someone post some code demonstrating actually using this?  I cannot see how it can be of any use.

TIA
template <typename T>
class TypeTraits
{
private:
	template<class U>
	struct UnConst
	{
		typedef U Result;
		enum { isConst = false };
	};
 
	template<class U>
	struct UnConst<const U>
	{
		typedef U Result;
		enum { isConst = true };
	};
 
public:
	enum { isConst = UnConst<T>::isConst };
	typedef typename UnConst<T>::Result NoneConstType;
};

Open in new window

Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Ok, say you have a generic function that takes a type of T and let's say that when the template is instantiated T is defined as a const type. Now, lets say that in that function we need to create a type of T that needs to be mutable. Because T is const this code will fail to compile. By using the const stripper it'll allow us to create a type of T that is not const even if T is.

Below is a contrived example.
template <typename T>
int foo(T & t)
{
	T x = T(); 
	++x; // This con't compile unless T is stripped of it's constness
 
	return x;
}
 
int main()
{
	int const n = 0;
	foo(n);
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ah, sorry evilrix. Took me longer to type than I thought ;)
>> Ah, sorry evilrix
No worries, a fresh perspective is always welcome I think :)
Not that it really matters as it's of no consequence to the point of the example (which was just to demonstrate that x won't be mutable if T is const), but line 4 was meant to be

T x = t;

:-)

template <typename T>
int foo(T & t)
{
	T x = t; 
	++x; // This con't compile unless T is stripped of it's constness
 
	return x;
}
 
int main()
{
	int const n = 0;
	foo(n);
}

Open in new window

Avatar of mrwad99

ASKER

Ah right.

I was getting confused again; forgetting that all this template stuff is checked at compile time - could not see the point in this when we have const_cast, but of course that is for runtime use!

Rx, forgive my potential ignorance, but your example does not appear to demonstrate the code I posted *being used*...?

I8:

I changed the code to use a simple class object, hence changed fun() to accept a T&.  During the assignment, we get the copy constructor called, which is understandable, but is this really necessary?  Surely a const_cast does not have this overhead, or am I wrong?
Avatar of mrwad99

ASKER

>> During the assignment,

i.e.

    typename TypeTraits<T>::NoneConstType tnc = t;
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
And in any case, for a const_cast, you need to know what to cast to. So, you'll need TypeTraits<T>::NoneConstType again ;)
Avatar of mrwad99

ASKER

As usual, many thanks :)
No problem ... Glad to be of assistance :)
>> No problem ... Glad to be of assistance
Ditto :)