|
|
Type traits #2
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: Compile time testing for "type equality"? - comp.lang.c++ ...Hi, I would like to know if there is a way to test if two types are indeed the ... You might have a look at is_same, from the Boost Type Traits library: http://www.boost ... Static reflection - a base for runtime reflection? - comp.lang.c++ ...> - iterate through qualifiers of fields (in fact it is sort of > possible now) - but why not it more neater to avoid generation of > intermediate types. Type traits already ... peek() vs unget(): which is better? - comp.lang.c++.moderated ...> > ----- > > token get_token(std::istream& is) > > { > > typedef std::istream::traits_type traits; > > char c; > > int i; > > // skip whitespaces > > while ... Variations On Formats On Arithmetic Scalar Types - comp.lang.c++ ...If std::type_traits<T>::is_iec559 is true, then T is required to conform to IEC 559 requirements, equivalent to IEEE 754. But is_iec559 is not required to be true. Why no std::back_insert_iterator::value_type? - comp.lang.c++ ...;-) Maybe this could be a solution: template <class T> struct true_value_type { typedef typename std::iterator_traits<T>::value_type type; }; template <class T> struct ... from std::string to std::istream? - comp.lang.c++peek() vs unget(): which is better? - comp.lang.c++.moderated ...----- token get_token(std::istream& is) { typedef std::istream::traits_type traits ... Correct way to define iterators - comp.lang.c++.moderated ...What is the best way to solve this is it to typedef all iterator traits such as random_access_iterator_tag, difference_type etc for every iterator I write like: class my ... signed/unsigned as fundamental types - comp.lang.c++.moderated ...... type for T. Allowing something like > you suggested would solve this problem for the basic types, but not > for user defined integral types. Some sort of traits class ... cannot write std::map via ostream_iterator? - comp.lang.c++ ...... string> map_ss; ostream& operator<<( ostream& os, const map_ss::value_type ... const > std::string, std::string>, _CharT = char, _Traits = > std::char_traits ... How to suppress template specialization? - comp.lang.c++.moderated ...I need to use bool type of vectors in in a code based on STL and and MPI2 library ... vector depending on the use case >> 2) you might need to specialize std::char_traits ... - Microsoft Corporation: Software, Smartphones ...Defines templates that provide compile-time constants that give information about the properties of their type arguments. C++ Type Traits | Dr Dobb'sGeneric programming does not have to sink to the lowest common denominator. And therein lies the value of type traits. 7/20/2012 6:10:48 AM
|
|
|
|
|
|
|
|
|