Type of a dereferenced pointer

Type of a dereferenced pointer

Post by Grizwo » Fri, 25 Jul 2003 09:10:09



How do you capture the type of a dereferenced pointer?

I'm trying to create a genaric functor that takes a unary_function
expecting a pointer argument and returns a unary_function that expects
the object or an object reference.

ie: When given a _Func such that _Func(obj*) is legal, it returns a
_Func such that _Func(obj) is legal.

Here's what I've got so far:

template <class _T1, class _F1>
class _Ref
  : public std::unary_function<*_F1::argument_type, _F1::result_type>
//   Need something legal here ^^^^^^^^^^^^^^^^^^^
{
public:
    _Ref(const _F1& t) : m_F1(t) { }

    result_type operator()(const *_F1::argument_type& _x) {
//                      and here ^^^^^^^^^^^^^^^^^^^
        return m_F1(&_x);
    }
private:
    _F1 m_F1;

Quote:};

Thanks in advance!

Chris Grenz

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

Type of a dereferenced pointer

Post by Emil Dotchevs » Sun, 27 Jul 2003 20:04:51


To get a T& from T* you can use std::iterator_traits:

   #include <iterator>

   typedef std::iterator_traits<int*>::value_type & int_ref;

If used in a template, you need to use typename:

   template <class T>
   struct ptr_to_type
   {
     typedef typename std::iterator_traits<T>::value_type type;
   };

   typedef ptr_to_type<int*>::type & int_ref;

Also, you should know that it is illegal to use identifiers that begin
with a _ followed by a capital letter (for example _Foo), or any
identifier containing a double _ (for example foo__bar). These are
reserved for use by the compiler and/or the standard libraries.

--Emil

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

Type of a dereferenced pointer

Post by Dhru » Mon, 28 Jul 2003 19:43:57


 > How do you capture the type of a dereferenced pointer?
 >
 > I'm trying to create a genaric functor that takes a unary_function
 > expecting a pointer argument and returns a unary_function that expects
 > the object or an object reference.
 >
 > ie: When given a _Func such that _Func(obj*) is legal, it returns a
 > _Func such that _Func(obj) is legal.
 >

Let me see, is this what you want:

#include <iostream>
#include <iterator>
#include <functional>

template <class Type, class Return>
struct test : public std::unary_function<Type, Return>
{
   bool operator() (Type t) { std::cout<<t<<std::endl; return true; }

Quote:};

//Partial specialization for pointers.
template <class Type, class Return>
struct test<Type*, Return> : public std::unary_function<Type*, Return>
{
   bool operator() (Type* t) { std::cout<<t<<std::endl; return true; }

Quote:};

struct give_unary_func {

template <class TYPE, class RETURN>
test<typename std::iterator_traits<TYPE>::value_type, RETURN> //To prevent slicing.
operator() (const std::unary_function<TYPE, RETURN>& uf) //To prevent slicing.
   {
     return test<typename std::iterator_traits<TYPE>::value_type, RETURN>();
   }

Quote:};

int main ()
{
   give_unary_func guf;
   test <int*, bool> t;
   int *ptr_int = new int(23);
   t (ptr_int);
   guf(t) (*ptr_int); //Here it is!!!

Quote:}

Regards,
-Dhruv.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

Type of a dereferenced pointer

Post by Carl Barr » Tue, 29 Jul 2003 19:00:49


 >
 > struct give_unary_func {
 >
 > template <class TYPE, class RETURN>
 > test<typename std::iterator_traits<TYPE>::value_type, RETURN> //To prevent
slicing.
 > operator() (const std::unary_function<TYPE, RETURN>& uf) //To prevent
slicing.
 >    {
 >      return test<typename std::iterator_traits<TYPE>::value_type, RETURN>();
 >    }
 > };
   Oucch does the standard really actually REQUIRE deriving from
std::unary_function or just providing typenames
argument_type,result_type to the functor?  A Solution that does not
suffer from requiring actual derivation from unary_function is:

template <class T> struct caller_traits;
// note caller_traits is complete only for pointer arguments...
template <class T> struct caller_traits<T *> {typedef T Result;};
template <class T> struct caller_traits<const T *>
{typedef const T Result;};

template <class F>
class caller
{
         F       f;
public:
         typedef typename F::result_type
result_type;
         typedef caller_traits<typename F::argument_type>::Result
&argument_type;  // fails for non pointer arguments....
         explicit caller(const F &a):f(a){}
         result_type operator ()(argument_type x) {return f(&x);}

Quote:};

template <class F>
inline caller<F> function_call(const F &f){return caller<F>(f);}

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

1. --Dereferencing point to incomplete type

Hallo,
I'm trying to compile a my kernel module regarding QoS-Linux. I need to
access to some information stored in the variable called "handle" and
"stats". But I get the error "Dereferencing point to incomplete type".
......What does it mean?

Thanks in advance for the help.
Marco

-------------------------
|        MODULE            |
-------------------------
#include <linux/kernel.h>
#include <linux/module.h>

#include <linux/netdevice.h>
#include <linux/pkt_sched.h>
#include <linux/pkt_cls.h>

int init_module()
{
struct device *device;
struct Qdisc *qdisc;
struct tc_stats stats;
u32 handle;

        device=dev_base;
        for(; device != NULL; device=device->next)
        {
                printk("\n------Scheda: %s ------\n",device->name);

        printk("Queue Discipline:\n");
        qdisc=device->qdisc;
        handle=qdisc->handle;   /* HERE IS THE ERROR*/
        stats=qdisc->stats;        /* HERE IS THE ERROR*/

        printk("Statistical of Qdisc:\n");
        printk("Handle=Major:%x
Minor:%x\n",TC_H_MAJ(handle),TC_H_MIN(handle));
        printk("DROP packet%d",stats.drops);
    .......
        }
return 0;

void cleanup_module()
{
        printk("...Auf Widersehen\n\n");

------------------------------------
|           STRUCT  (Simplified)       |
------------------------------------
Inside  <linux/pkt_sched.h> there is:
struct Qdisc
{
 struct Qdisc_head h;
 u32                          handle;
 struct tc_stats           stats;

--------------------------------------
Inside  <linux/netdevice.h> there is:
struct device
{
 char   *name;
 struct device  *next;
 struct Qdisc  *qdisc;

2. Documentation for 2.0

3. inferring dereference return type for input iterator

4. Setting up FTP server with win2000 server

5. Dereference a pointer of a pointerfield

6. Lost Connection to MySQL server during query

7. Is dereferencing a null pointer ok?

8. Tape Drive compatibility

9. does null pointer dereference throw?

10. Pointer Dereference Question

11. confused about pointer to member dereferencing

12. dereferencing deleted pointer

13. Throwing dereferenced pointers invokes copy constructor