inspecting templates and boost::is_pointer
Hello all, I am trying to specialize boost::is_pointer so, that it can determine if a type is a pointer wrapper (shared_ptr, auto_ptr, weak_ptr). Does anybody has ideas how this can be done? May be it would be possible to handle if e.g. one can determine if a class/struct has some typedef inside: template<...> class shared_ptr { typedef ... element_type; }; class some_other_class { typedef ... type; }; class some_other_class2 { //no typedef... }; Somehow I am not certain if I am on the right path. My problem is that I have a variant container type with different types. One could access a value using -> operator. The operator returns the pointer to the value. But if this value is a pointer (that can be determined by boost::is_pointer type trait) it should return the pointer and not pointer to pointer value. Now what happens if I store shared_ptr or any other type which overload -> operator? This will result in compiler error, because the compiler tries to access member of the pointer to pointer and not of the pointer instance. Is there may be a way to determine if the member access operator is overloaded? What if -> operator is defined in the base? And what if child class inherits base as protected or private? The other solution would be to force every user to write a specialized class is_pointer for every type stored in variant, but this would be not so nice. With Kind Regards, Ovanes Markarian
"Ovanes Markarian"
Hello all,
I am trying to specialize boost::is_pointer so, that it can determine if a type is a pointer wrapper (shared_ptr, auto_ptr, weak_ptr).
First of all, don't do that. It's a really bad idea, and it will probably break some (Boost) library code. is_pointer is supposed to detect real pointers, and if you start changing what it does with other types, you'll break the expectations of every bit of code that currently uses is_pointer. This has come up before, so maybe it should be a FAQ (John?)
Does anybody has ideas how this can be done? May be it would be possible to handle if e.g. one can determine if a class/struct has some typedef inside:
http://www.boost.org/libs/mpl/doc/refmanual/has-xxx-trait-def.html -- Dave Abrahams Boost Consulting www.boost-consulting.com
Dave,
thanks for your answer. I think this should be definitely a part of FAQ,
otherwise someone could spent endless hours on matters why some of boost
classes do not work as expected.
I have one more question. I tried the following code with VC 8.0:
#include
Hello all,
I am trying to specialize boost::is_pointer so, that it can determine if a type is a pointer wrapper (shared_ptr, auto_ptr, weak_ptr).
First of all, don't do that. It's a really bad idea, and it will probably break some (Boost) library code. is_pointer is supposed to detect real pointers, and if you start changing what it does with other types, you'll break the expectations of every bit of code that currently uses is_pointer. This has come up before, so maybe it should be a FAQ (John?)
Does anybody has ideas how this can be done? May be it would be possible to handle if e.g. one can determine if a class/struct has some typedef inside:
http://www.boost.org/libs/mpl/doc/refmanual/has-xxx-trait-def.html -- Dave Abrahams Boost Consulting www.boost-consulting.com
"Ovanes Markarian"
Dave,
thanks for your answer. I think this should be definitely a part of FAQ, otherwise someone could spent endless hours on matters why some of boost classes do not work as expected.
I have one more question. I tried the following code with VC 8.0:
#include
struct test2 { typedef int xxx; }; BOOST_MPL_HAS_XXX_TRAIT_DEF(has_xxx) typedef has_xxx my_test;
And got a compiler error:
main.cpp(22) : error C2146: syntax error : missing ';' before identifier 'my_test'
Somehow the macro was not expanded. I replaced xxx with some other name before but end up in compiler errors, so that I decided to check if the standard example is compilable at all by using the typedef. Do you have any suggestions?
Oh, the example in the docs is clearly wrong (a job for LiTRe, Aleksey?) Read the docs but ignore the example. You want BOOST_MPL_HAS_XXX_TRAIT_DEF(xxx) But then has_xxx will be a unary metafunction, not a type, so the typedef you have there is illegal. typedef has_xxx<test2> my_test; BOOST_MPL_ASSERT((my_test));
Thanks, Ovanes
-----Original Message----- From: David Abrahams [mailto:dave@boost-consulting.com] Sent: Saturday, June 24, 2006 04:51 To: boost-users@lists.boost.org Subject: Re: [Boost-users] inspecting templates and boost::is_pointer
"Ovanes Markarian"
writes: Hello all,
I am trying to specialize boost::is_pointer so, that it can determine if a type is a pointer wrapper (shared_ptr, auto_ptr, weak_ptr).
First of all, don't do that. It's a really bad idea, and it will probably break some (Boost) library code. is_pointer is supposed to detect real pointers, and if you start changing what it does with other types, you'll break the expectations of every bit of code that currently uses is_pointer. This has come up before, so maybe it should be a FAQ (John?)
Does anybody has ideas how this can be done? May be it would be possible to handle if e.g. one can determine if a class/struct has some typedef inside:
http://www.boost.org/libs/mpl/doc/refmanual/has-xxx-trait-def.html
-- Dave Abrahams Boost Consulting www.boost-consulting.com
-- Dave Abrahams Boost Consulting www.boost-consulting.com
participants (3)
-
David Abrahams
-
John Maddock
-
Ovanes Markarian