473,670 Members | 2,405 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ptr_policy class template type

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<ch ar*, implicit_conver sion_policy<cha r*> >
scope_handle<FI LE, implicit_conver sion_policy<FIL E> >
scope_handle<HA NDLE, no_policy<HANDL E> >

and for ptr_policy

scope_handle<My node*, ptr_policy<Myno de> >

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<My node*, ptr_policy<Myno de*> >

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<typena me 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 *******

Oct 19 '05 #1
5 1519
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<ch ar*, implicit_conver sion_policy<cha r*> >
scope_handle<FI LE, implicit_conver sion_policy<FIL E> >
scope_handle<HA NDLE, no_policy<HANDL E> >

and for ptr_policy

scope_handle<My node*, ptr_policy<Myno de> >

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<My node*, ptr_policy<Myno de*> >

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<typena me 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>::typ e_t type_t;
typedef type_t& ref_t;

static ref_t deref(T *t) { return deref_t<T>::der ef(*t); }
};

template <typename T>
typename deref_t<T>::ref _t deref(T &t) {
return deref_t<T>::der ef(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.
Oct 19 '05 #2
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<ch ar*, implicit_conver sion_policy<cha r*> >
scope_handle<FI LE, implicit_conver sion_policy<FIL E> >
scope_handle<HA NDLE, no_policy<HANDL E> >

and for ptr_policy

scope_handle<My node*, ptr_policy<Myno de> >

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<My node*, ptr_policy<Myno de*> >

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<typena me 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>::typ e_t type_t;
typedef type_t& ref_t;

static ref_t deref(T *t) { return deref_t<T>::der ef(*t); }
};

template <typename T>
typename deref_t<T>::ref _t deref(T &t) {
return deref_t<T>::der ef(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.

Oct 19 '05 #3

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-

Oct 19 '05 #4

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<ch ar*, implicit_conver sion_policy<cha r*> >
scope_handle<FI LE, implicit_conver sion_policy<FIL E> >
scope_handle<HA NDLE, no_policy<HANDL E> >

and for ptr_policy

scope_handle<My node*, ptr_policy<Myno de> >

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<My node*, ptr_policy<Myno de*> >

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<typena me 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>::typ e_t type_t;
typedef type_t& ref_t;

static ref_t deref(T *t) { return deref_t<T>::der ef(*t); }
};

template <typename T>
typename deref_t<T>::ref _t deref(T &t) {
return deref_t<T>::der ef(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.Typetrait s
(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

Oct 19 '05 #5
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<ch ar*, implicit_conver sion_policy<cha r*> >
scope_handle<FI LE, implicit_conver sion_policy<FIL E> >
scope_handle<HA NDLE, no_policy<HANDL E> >

and for ptr_policy

scope_handle<My node*, ptr_policy<Myno de> >

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<My node*, ptr_policy<Myno de*> >

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<typena me 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>::typ e_t type_t;
typedef type_t& ref_t;

static ref_t deref(T *t) { return deref_t<T>::der ef(*t); }
};

template <typename T>
typename deref_t<T>::ref _t deref(T &t) {
return deref_t<T>::der ef(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<typena me 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>::ty pe_t type_t;};
typedef typename deref_t<T>::typ e_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!

Oct 19 '05 #6

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

Similar topics

4
2023
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 function main() Here is the relevant portion of my template class //***************************************************************************
13
2816
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 { }; template <typename Base> class Tpl : protected Base {
2
2033
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" facility. I am to get rid of them and replace them with exceptions. I need to create a "linkedListException" class that's declared and implemented in my "linkedListType" class. This class needs to inherit from the base "exception" class and return...
2
1422
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 should always be derived. I want the template to have a compile-time check of the type, but I don't want a "full" template class because all generic methods will be generated for every type I use this class with, which is a waste of resources.
9
2307
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 same type for a given instance of the class, but different types for different instances.) #include<set> using namespace std; template<class T>
6
3998
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 as well. To support thin-template, I need to make virtual function as inline. Now, I know that compiler would generate an out-of-line copy when it
17
2444
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, typename V> Vector<U_or_V> operator+ ( const Vector<U>&, const Vector<V>&);
4
3634
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 inheritance (avoiding prosperation of virtual func tables). -- At the same time, a class taking N types shall contain a virtual member function that calls a function according to the number of arguments That means, something like:
4
3091
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) { T retVal = val; if ( retVal < vmin ) retVal = vmin;
0
8466
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8813
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8591
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8659
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6212
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4208
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4388
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2799
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 we have to send another system
2
2037
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.