I'm fine tuning a scope_handle class that takes a policy class as the
second template. http://code.axter.com/scope_handle.h
Please see above link for full understanding of the problem.
One thing I don't like about the way the current policy template is
setup is that for the ptr_policy class the first template type is
different from the template type given to the policy.
And on the other policy classes, the template type is the same. (Which
is what I prefer)
Example:
scope_handle<char*, implicit_conversion_policy<char*> >
scope_handle<FILE, implicit_conversion_policy<FILE> >
scope_handle<HANDLE, no_policy<HANDLE> >
and for ptr_policy
scope_handle<Mynode*, ptr_policy<Mynode> >
I would like to be able to setup the ptr_policy class so that it takes
the same type as the first template type.
scope_handle<Mynode*, ptr_policy<Mynode*> >
But then if I do that, I can't seem to figure out a way to get the
dereference type declaration on the T& operator*() function.
template<typename T>
class ptr_policy
{
protected:
typedef typename T* type_t;
ptr_policy(type_t type):m_handle(type){}
type_t m_handle;
public:
type_t operator->() const{return m_handle;}
T& operator*() const{return *m_handle;}
bool operator! () const{return m_handle == 0;}
};
If T is of type (foo*) instead of (foo), is there any way to get type
(foo&) out of type (foo*)?
FYI: ******* I'm not asking about dereferencing the variable. I'm
referring to how to dereference the TYPE ******* 5 1510
Axter wrote: I'm fine tuning a scope_handle class that takes a policy class as the second template. http://code.axter.com/scope_handle.h Please see above link for full understanding of the problem.
One thing I don't like about the way the current policy template is setup is that for the ptr_policy class the first template type is different from the template type given to the policy. And on the other policy classes, the template type is the same. (Which is what I prefer) Example: scope_handle<char*, implicit_conversion_policy<char*> > scope_handle<FILE, implicit_conversion_policy<FILE> > scope_handle<HANDLE, no_policy<HANDLE> >
and for ptr_policy
scope_handle<Mynode*, ptr_policy<Mynode> >
I would like to be able to setup the ptr_policy class so that it takes the same type as the first template type.
scope_handle<Mynode*, ptr_policy<Mynode*> >
But then if I do that, I can't seem to figure out a way to get the dereference type declaration on the T& operator*() function.
template<typename T> class ptr_policy { protected: typedef typename T* type_t; ptr_policy(type_t type):m_handle(type){} type_t m_handle; public: type_t operator->() const{return m_handle;} T& operator*() const{return *m_handle;} bool operator! () const{return m_handle == 0;} };
If T is of type (foo*) instead of (foo), is there any way to get type (foo&) out of type (foo*)?
FYI: ******* I'm not asking about dereferencing the variable. I'm referring to how to dereference the TYPE *******
OK, I'm interpreting this to mean that you'll have some "template
<typename T>", and you want to be able to dereference that, whether it's
an int, an int*, or an int*****. If not, everything I am about to say
is completely worthless.
The way in which I would do this is as follows:
template <typename T>
struct deref_t {
typedef T type_t;
typedef type_t& ref_t;
static ref_t deref(ref_t t) { return t; }
};
template <typename T>
struct deref_t<T*> {
typedef typename deref_t<T>::type_t type_t;
typedef type_t& ref_t;
static ref_t deref(T *t) { return deref_t<T>::deref(*t); }
};
template <typename T>
typename deref_t<T>::ref_t deref(T &t) {
return deref_t<T>::deref(t);
}
So, what happens goes something like this. We call the function
"deref(p)". The template wrapping that function detects our type, fills
in the appropriate return type [this is important; the trickiest part of
this is getting the return type right], and calls the function inside of
our specialized wrapper class.
Our wrapper class does straight forward recursion with templates. If p
is a pointer, it calls itself on *p. If p is not a pointer, it returns.
Jack Saalweachter wrote: Axter wrote: I'm fine tuning a scope_handle class that takes a policy class as the second template. http://code.axter.com/scope_handle.h Please see above link for full understanding of the problem.
One thing I don't like about the way the current policy template is setup is that for the ptr_policy class the first template type is different from the template type given to the policy. And on the other policy classes, the template type is the same. (Which is what I prefer) Example: scope_handle<char*, implicit_conversion_policy<char*> > scope_handle<FILE, implicit_conversion_policy<FILE> > scope_handle<HANDLE, no_policy<HANDLE> >
and for ptr_policy
scope_handle<Mynode*, ptr_policy<Mynode> >
I would like to be able to setup the ptr_policy class so that it takes the same type as the first template type.
scope_handle<Mynode*, ptr_policy<Mynode*> >
But then if I do that, I can't seem to figure out a way to get the dereference type declaration on the T& operator*() function.
template<typename T> class ptr_policy { protected: typedef typename T* type_t; ptr_policy(type_t type):m_handle(type){} type_t m_handle; public: type_t operator->() const{return m_handle;} T& operator*() const{return *m_handle;} bool operator! () const{return m_handle == 0;} };
If T is of type (foo*) instead of (foo), is there any way to get type (foo&) out of type (foo*)?
FYI: ******* I'm not asking about dereferencing the variable. I'm referring to how to dereference the TYPE ******* OK, I'm interpreting this to mean that you'll have some "template <typename T>", and you want to be able to dereference that, whether it's an int, an int*, or an int*****. If not, everything I am about to say is completely worthless.
The way in which I would do this is as follows: template <typename T> struct deref_t { typedef T type_t; typedef type_t& ref_t;
static ref_t deref(ref_t t) { return t; } };
template <typename T> struct deref_t<T*> { typedef typename deref_t<T>::type_t type_t; typedef type_t& ref_t;
static ref_t deref(T *t) { return deref_t<T>::deref(*t); } };
template <typename T> typename deref_t<T>::ref_t deref(T &t) { return deref_t<T>::deref(t); }
So, what happens goes something like this. We call the function "deref(p)". The template wrapping that function detects our type, fills in the appropriate return type [this is important; the trickiest part of this is getting the return type right], and calls the function inside of our specialized wrapper class.
Our wrapper class does straight forward recursion with templates. If p is a pointer, it calls itself on *p. If p is not a pointer, it returns.
Thanks for trying to answer my question, but that's not exactly what
I'm looking for.
I need to be able to get type (int) when the template type is (int*).
Or type (int&) when the template type is (int*).
I can get type (int*) or type (int&) when the template type is (int).
But I want to be able to do it the other way around.
Axter wrote: Thanks for trying to answer my question, but that's not exactly what I'm looking for. I need to be able to get type (int) when the template type is (int*). Or type (int&) when the template type is (int*).
I can get type (int*) or type (int&) when the template type is (int). But I want to be able to do it the other way around.
Jack's solution already does this. E.g.:
typedef deref_t<int*>::type_t Int;
Int i = 0; // equivalent to "int i = 0;"
Hope this helps,
-shez-
Axter wrote: Jack Saalweachter wrote: Axter wrote: I'm fine tuning a scope_handle class that takes a policy class as the second template. http://code.axter.com/scope_handle.h Please see above link for full understanding of the problem.
One thing I don't like about the way the current policy template is setup is that for the ptr_policy class the first template type is different from the template type given to the policy. And on the other policy classes, the template type is the same. (Which is what I prefer) Example: scope_handle<char*, implicit_conversion_policy<char*> > scope_handle<FILE, implicit_conversion_policy<FILE> > scope_handle<HANDLE, no_policy<HANDLE> >
and for ptr_policy
scope_handle<Mynode*, ptr_policy<Mynode> >
I would like to be able to setup the ptr_policy class so that it takes the same type as the first template type.
scope_handle<Mynode*, ptr_policy<Mynode*> >
But then if I do that, I can't seem to figure out a way to get the dereference type declaration on the T& operator*() function.
template<typename T> class ptr_policy { protected: typedef typename T* type_t; ptr_policy(type_t type):m_handle(type){} type_t m_handle; public: type_t operator->() const{return m_handle;} T& operator*() const{return *m_handle;} bool operator! () const{return m_handle == 0;} };
If T is of type (foo*) instead of (foo), is there any way to get type (foo&) out of type (foo*)?
FYI: ******* I'm not asking about dereferencing the variable. I'm referring to how to dereference the TYPE ******* OK, I'm interpreting this to mean that you'll have some "template <typename T>", and you want to be able to dereference that, whether it's an int, an int*, or an int*****. If not, everything I am about to say is completely worthless.
The way in which I would do this is as follows: template <typename T> struct deref_t { typedef T type_t; typedef type_t& ref_t;
static ref_t deref(ref_t t) { return t; } };
template <typename T> struct deref_t<T*> { typedef typename deref_t<T>::type_t type_t; typedef type_t& ref_t;
static ref_t deref(T *t) { return deref_t<T>::deref(*t); } };
template <typename T> typename deref_t<T>::ref_t deref(T &t) { return deref_t<T>::deref(t); }
So, what happens goes something like this. We call the function "deref(p)". The template wrapping that function detects our type, fills in the appropriate return type [this is important; the trickiest part of this is getting the return type right], and calls the function inside of our specialized wrapper class.
Our wrapper class does straight forward recursion with templates. If p is a pointer, it calls itself on *p. If p is not a pointer, it returns.
Thanks for trying to answer my question, but that's not exactly what I'm looking for. I need to be able to get type (int) when the template type is (int*). Or type (int&) when the template type is (int*).
I can get type (int*) or type (int&) when the template type is (int). But I want to be able to do it the other way around.
I also think Jack's post gives you what you want. Compare also the type
trait facilities in Boost.Typetraits
( http://boost.org/doc/html/boost_typetraits.html, esp. remove_pointer
and the like) and in Loki ( http://sourceforge.net/projects/loki-lib/).
Cheers! --M
Jack Saalweachter wrote: Axter wrote: I'm fine tuning a scope_handle class that takes a policy class as the second template. http://code.axter.com/scope_handle.h Please see above link for full understanding of the problem.
One thing I don't like about the way the current policy template is setup is that for the ptr_policy class the first template type is different from the template type given to the policy. And on the other policy classes, the template type is the same. (Which is what I prefer) Example: scope_handle<char*, implicit_conversion_policy<char*> > scope_handle<FILE, implicit_conversion_policy<FILE> > scope_handle<HANDLE, no_policy<HANDLE> >
and for ptr_policy
scope_handle<Mynode*, ptr_policy<Mynode> >
I would like to be able to setup the ptr_policy class so that it takes the same type as the first template type.
scope_handle<Mynode*, ptr_policy<Mynode*> >
But then if I do that, I can't seem to figure out a way to get the dereference type declaration on the T& operator*() function.
template<typename T> class ptr_policy { protected: typedef typename T* type_t; ptr_policy(type_t type):m_handle(type){} type_t m_handle; public: type_t operator->() const{return m_handle;} T& operator*() const{return *m_handle;} bool operator! () const{return m_handle == 0;} };
If T is of type (foo*) instead of (foo), is there any way to get type (foo&) out of type (foo*)?
FYI: ******* I'm not asking about dereferencing the variable. I'm referring to how to dereference the TYPE ******* OK, I'm interpreting this to mean that you'll have some "template <typename T>", and you want to be able to dereference that, whether it's an int, an int*, or an int*****. If not, everything I am about to say is completely worthless.
The way in which I would do this is as follows: template <typename T> struct deref_t { typedef T type_t; typedef type_t& ref_t;
static ref_t deref(ref_t t) { return t; } };
template <typename T> struct deref_t<T*> { typedef typename deref_t<T>::type_t type_t; typedef type_t& ref_t;
static ref_t deref(T *t) { return deref_t<T>::deref(*t); } };
template <typename T> typename deref_t<T>::ref_t deref(T &t) { return deref_t<T>::deref(t); }
So, what happens goes something like this. We call the function "deref(p)". The template wrapping that function detects our type, fills in the appropriate return type [this is important; the trickiest part of this is getting the return type right], and calls the function inside of our specialized wrapper class.
Our wrapper class does straight forward recursion with templates. If p is a pointer, it calls itself on *p. If p is not a pointer, it returns.
Sorry for my previous post, but I now see that your proposed method is
what I'm looking for.
I really didn't understand it, and to be honest, I still don't fully
understand what is making that work.
Here's the modify class using the method you posted:
template<typename T>
class ptr_policy
{
protected:
template <typename TT> struct deref_t {typedef TT type_t;};
template <typename TT> struct deref_t<TT*> {typedef typename
deref_t<TT>::type_t type_t;};
typedef typename deref_t<T>::type_t ref_t;
typedef typename T type_t;
ptr_policy(type_t type):m_handle(type){}
type_t m_handle;
public:
type_t operator->() const{return m_handle;}
ref_t& operator*() const{return *m_handle;}
bool operator! () const{return m_handle == 0;}
};
Thank you very much! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Zenon |
last post by:
Folks,
I have been trying for a week but I cannot debug the following error:
Error E2285 ex5.cpp 141: Could not find a match for
'matrix<complex<double>>::operator =(complex<double>)' in...
|
by: Walt Karas |
last post by:
The following gives an error in the declaration of the
member function x() of the class template Tpl, compiliing
with a recent version of GCC under Solaris:
class A { };
class B { };
...
|
by: dinks |
last post by:
Hi,
I'm new to C++ and have been assigned a task which i dont completely
understand. Any help would be greately appreciated.
Here is the problem:
The class "linkedListType" use the "assert"...
|
by: Capstar |
last post by:
Hi NG,
Is it possible to make a template class, which has only one method that
makes use of the template type. The rest of the methods will work on the
base class of which the template type...
|
by: Jon Wilson |
last post by:
I have a class which needs to accumulate data. The way we get this data
is by calling a member function which returns float on a number of
different objects of different type (they are all the...
| |
by: RainBow |
last post by:
Greetings!!
I introduced the so-called "thin-template" pattern for controlling the
code bloat caused due to template usage.
However, one of the functions in the template happens to be virtual...
|
by: benben |
last post by:
Given a class template Vector<>, I would like to overload operator +.
But I have a hard time deciding whether the return type should be
Vector<U> or Vector<V>, as in:
template <typename U,...
|
by: Frank-René Schäfer |
last post by:
-- A class needs to have N members according to N types
mentioned in a typelist (possibly with one type occuring more than
once).
-- The classes should be generated **avoiding** multiple...
|
by: Dan Krantz |
last post by:
I have the following template to ensure that a given number (val) falls into
a range (between vmin & vmax):
template<typename T> T ForceNumericRange( const T& val, const T& vmin, const
T& vmax)...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |