Type traits #2

  • Follow


Hi:

I am following the doc http://boost.cowic.de/rc/pdf/type_traits.pdf
and trying to do the example from pg 4. I want to create my own
'is_pointer' trait. I have done this as shown in code below, and am
trying to test it as shown below.

When I don't have the 'debug0' & 'debug1' statements in my is_pointer
templates, I can compile. But when I do have them, I get compile
errors. What am I missing - and how can I test my code ?

I want to see if 'a' is a pointer.

Plz advise.

Thanks
Asif

====


#include <iostream>
#include <boost/type_traits.hpp>
using namespace boost;
using namespace std;



namespace my_namespace
{

	template <typename T> struct is_pointer     : public false_type
{  cout << "debug0" << endl; };
	template <typename T> struct is_pointer<T*> : public true_type { cout
<< "debug1" << endl;  };
}


int main ( )
  {
       my_namespace::is_pointer<int> a;

  return 0;
  }
0
Reply asifnzaidi (12) 3/1/2010 4:47:57 PM

On Mar 1, 8:47=A0am, Asif Zaidi <asifnza...@gmail.com> wrote:
> Hi:
>
> I am following the dochttp://boost.cowic.de/rc/pdf/type_traits.pdf
> and trying to do the example from pg 4. I want to create my own
> 'is_pointer' trait. I have done this as shown in code below, and am
> trying to test it as shown below.
>
> When I don't have the 'debug0' & 'debug1' statements in my is_pointer
> templates, I can compile. But when I do have them, I get compile
> errors. What am I missing - and how can I test my code ?
>
> I want to see if 'a' is a pointer.
>
> Plz advise.
>
> Thanks
> Asif
>
> =3D=3D=3D=3D
>
> #include <iostream>
> #include <boost/type_traits.hpp>
> using namespace boost;
> using namespace std;
>
> namespace my_namespace
> {
>
> =A0 =A0 =A0 =A0 template <typename T> struct is_pointer =A0 =A0 : public =
false_type
> { =A0cout << "debug0" << endl; };
> =A0 =A0 =A0 =A0 template <typename T> struct is_pointer<T*> : public true=
_type { cout
> << "debug1" << endl; =A0};
>
> }

You can't have executable statements directly as part of a class.
They need
to be part of a member function.

Try:
    template <typename T> struct is_pointer :
      public false_type
    {
       is_pointer() {  cout << "debug0" << endl; }
    };
    template <typename T> struct is_pointer<T*> :
     public true_type
    {
       is_pointer() { cout<< "debug1" << endl; }
    };
>
> int main ( )
> =A0 {
> =A0 =A0 =A0 =A0my_namespace::is_pointer<int> a;
>
> =A0 return 0;
> =A0 }

0
Reply red 3/1/2010 6:03:45 PM


Great.. thank you.
I don't know why I didn't see that !!

I will probably have more questions later  on


Thanks

Asif



On Mar 1, 12:03=A0pm, red floyd <redfl...@gmail.com> wrote:
> On Mar 1, 8:47=A0am, Asif Zaidi <asifnza...@gmail.com> wrote:
>
>
>
> > Hi:
>
> > I am following the dochttp://boost.cowic.de/rc/pdf/type_traits.pdf
> > and trying to do the example from pg 4. I want to create my own
> > 'is_pointer' trait. I have done this as shown in code below, and am
> > trying to test it as shown below.
>
> > When I don't have the 'debug0' & 'debug1' statements in my is_pointer
> > templates, I can compile. But when I do have them, I get compile
> > errors. What am I missing - and how can I test my code ?
>
> > I want to see if 'a' is a pointer.
>
> > Plz advise.
>
> > Thanks
> > Asif
>
> > =3D=3D=3D=3D
>
> > #include <iostream>
> > #include <boost/type_traits.hpp>
> > using namespace boost;
> > using namespace std;
>
> > namespace my_namespace
> > {
>
> > =A0 =A0 =A0 =A0 template <typename T> struct is_pointer =A0 =A0 : publi=
c false_type
> > { =A0cout << "debug0" << endl; };
> > =A0 =A0 =A0 =A0 template <typename T> struct is_pointer<T*> : public tr=
ue_type { cout
> > << "debug1" << endl; =A0};
>
> > }
>
> You can't have executable statements directly as part of a class.
> They need
> to be part of a member function.
>
> Try:
> =A0 =A0 template <typename T> struct is_pointer :
> =A0 =A0 =A0 public false_type
> =A0 =A0 {
> =A0 =A0 =A0 =A0is_pointer() { =A0cout << "debug0" << endl; }
> =A0 =A0 };
> =A0 =A0 template <typename T> struct is_pointer<T*> :
> =A0 =A0 =A0public true_type
> =A0 =A0 {
> =A0 =A0 =A0 =A0is_pointer() { cout<< "debug1" << endl; }
> =A0 =A0 };
>
>
>
> > int main ( )
> > =A0 {
> > =A0 =A0 =A0 =A0my_namespace::is_pointer<int> a;
>
> > =A0 return 0;
> > =A0 }
>
>

0
Reply Asif 3/2/2010 12:45:22 AM

On 3/1/2010 4:45 PM, Asif Zaidi wrote:
> Great.. thank you.
> I don't know why I didn't see that !!
>
> I will probably have more questions later  on
>
>
> Thanks
>
[remainder redacted].

You're welcome.

By the way, please don't top-post.  See FAQ 5.4
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.4
0
Reply red 3/2/2010 4:43:32 AM

3 Replies
245 Views

(page loaded in 0.094 seconds)

Similiar Articles:













7/20/2012 6:10:48 AM


Reply: