Re: [Boost-users] detect if a type has a size_t member

-----Original Message----- From: Hicham Mouline [mailto:hicham@mouline.org] Sent: 05 July 2009 15:36 To: Hicham Mouline Subject: FW: [Boost-users] detect if a type has a size_t member
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of joel Sent: 03 July 2009 19:04 To: boost-users@lists.boost.org Subject: Re: [Boost-users] detect if a type has a size_t member
Hicham Mouline wrote:
Can I put the macro inside the struct S ? I guess not. I'd write: HAS_STATIC_MEMBER_NAMED(T, value)
which would return a integral bool or a bool type it works liek BOOST_MPL_HAS_XXXX so put it where you want to define a boolean meta-function. It supports ::value and ::type return mpl boolean. I'm sorry I can quite get how to use it. I have this:
#include <iostream> #include <boost/introspection/has_member_data.hpp> struct S { static const size_t maxsize =5; }; struct T { }; BOOST_HAS_MEMBER_DATA(S, maxsize) BOOST_HAS_MEMBER_DATA(T, maxsize) int main() { if ( boost::introspection::has_member_data_maxsize<T>::value ) { std::cout<<" T has max_size"<<std::endl; } return 0; } which doesn't build because error C2953: 'boost::introspection::has_member_data_maxsize' : class template has already been defined Hicham

Hicham Mouline wrote:
which doesn't build because
error C2953: 'boost::introspection::has_member_data_maxsize' : class template has already been defined
The macro build a template meta-function that you use on your type : BOOST_HAS_MEMBER_DATA(size_t, maxsize); int main() { if ( boost::introspection::has_member_data_maxsize<T>::value ) { std::cout<<" T has max_size"<<std::endl; } return 0; } ___________________________________________ Joel Falcou - Assistant Professor PARALL Team - LRI - Universite Paris Sud XI Tel : (+33)1 69 15 66 35

-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of joel Sent: 06 July 2009 14:26 To: boost-users@lists.boost.org Subject: Re: [Boost-users] detect if a type has a size_t member
Hicham Mouline wrote:
which doesn't build because
error C2953: 'boost::introspection::has_member_data_maxsize' : class template has already been defined
The macro build a template meta-function that you use on your type :
BOOST_HAS_MEMBER_DATA(size_t, maxsize);
int main() { if ( boost::introspection::has_member_data_maxsize<T>::value ) { std::cout<<" T has max_size"<<std::endl; } return 0; }
Thank you. May I suggest to replace "Type" in #define BOOST_HAS_MEMBER_DATA(Type,Name) in the header file by MemberType for e.g. A naive user (myself) thought "Type" was his type, not the member data type. Regards,

Hicham Mouline wrote:
Thank you. May I suggest to replace "Type" in #define BOOST_HAS_MEMBER_DATA(Type,Name)
in the header file by MemberType for e.g.
A naive user (myself) thought "Type" was his type, not the member data type Will do :)
-- ___________________________________________ Joel Falcou - Assistant Professor PARALL Team - LRI - Universite Paris Sud XI Tel : (+33)1 69 15 66 35

-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of joel Sent: 06 July 2009 14:26 To: boost-users@lists.boost.org Subject: Re: [Boost-users] detect if a type has a size_t member
Hicham Mouline wrote:
which doesn't build because
error C2953: 'boost::introspection::has_member_data_maxsize' : class template has already been defined
The macro build a template meta-function that you use on your type :
BOOST_HAS_MEMBER_DATA(size_t, maxsize);
int main() { if ( boost::introspection::has_member_data_maxsize<T>::value ) { std::cout<<" T has max_size"<<std::endl; } return 0; } Thank you,
1) struct S { static const size_t maxsize =5; }; 2) struct S { static size_t maxsize; }; 3) struct S { size_t maxsize; }; Applied to S, the code doesn't compile in 1 and returns false in 2. Is there a fix that can make it work for 1, 2 and 3 ? Regards,

Hicham Mouline wrote:
BOOST_HAS_MEMBER_DATA(size_t, maxsize);
int main() { if ( boost::introspection::has_member_data_maxsize<T>::value ) { std::cout<<" T has max_size"<<std::endl; } return 0; } Thank you,
1) struct S { static const size_t maxsize =5; };
2) struct S { static size_t maxsize; };
3) struct S { size_t maxsize; };
Applied to S, the code doesn't compile in 1 and returns false in 2.
Is there a fix that can make it work for 1, 2 and 3 ?
No. Static members and non-static ones are very different things, and each requires usage of a different macro. BOOST_HAS_STATIC_MEMBER_DATA and BOOST_HAS_MEMBER_DATA.

2009/7/29 Mathias Gaunard <mathias.gaunard@ens-lyon.org>
1) struct S {
static const size_t maxsize =5; };
2) struct S { static size_t maxsize; };
3) struct S { size_t maxsize; };
Applied to S, the code doesn't compile in 1 and returns false in 2.
Is there a fix that can make it work for 1, 2 and 3 ?
No. Static members and non-static ones are very different things, and each requires usage of a different macro. BOOST_HAS_STATIC_MEMBER_DATA and BOOST_HAS_MEMBER_DATA.
It explains why 2 returns false, but does not explain why 1 does not compile. Roman Perepelitsa.

Roman Perepelitsa wrote:
2009/7/29 Mathias Gaunard <mathias.gaunard@ens-lyon.org <mailto:mathias.gaunard@ens-lyon.org>>
1) struct S { static const size_t maxsize =5; };
It explains why 2 returns false, but does not explain why 1 does not compile.
Because static const <integral type> = <some value> members are quite special beasts. Indeed, unlike other static members, you can't take their address, since they don't exist in memory but only as compile-time constants. Testing whether the type is integral is not enough either, since if there is no definition they behave like normal static members.

Mathias Gaunard wrote:
It explains why 2 returns false, but does not explain why 1 does not compile. Introspection is also in the vault as a WIP. Straneg behavior under soem compilers have to be expected. I'm working on this quite extensively.
-- ___________________________________________ Joel Falcou - Assistant Professor PARALL Team - LRI - Universite Paris Sud XI Tel : (+33)1 69 15 66 35

AMDG Mathias Gaunard wrote:
Because static const <integral type> = <some value> members are quite special beasts. Indeed, unlike other static members, you can't take their address, since they don't exist in memory but only as compile-time constants.
Yes you can take the address of a static const integral type. If you do, you are required to have a namespace scope definition, just like an ordinary member, but failure to do so, can only cause a linker error. It won't affect the compiler. In Christ, Steven Watanabe

On 7/29/09, Mathias Gaunard <mathias.gaunard@ens-lyon.org> wrote:
Because static const <integral type> = <some value> members are quite special beasts.
May be that's why in "The C++ Programming Language, 3ed", Bjarne says about them, "I consider this a misfeature. When you need a symbolic constant within a class declaration, use an enumerator." Regards, -Asif

-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of Mathias Gaunard Sent: 29 July 2009 13:12 To: boost-users@lists.boost.org Subject: Re: [Boost-users] detect if a type has a size_t member
Hicham Mouline wrote:
BOOST_HAS_MEMBER_DATA(size_t, maxsize);
int main() { if ( boost::introspection::has_member_data_maxsize<T>::value ) { std::cout<<" T has max_size"<<std::endl; } return 0; } Thank you,
1) struct S { static const size_t maxsize =5; };
2) struct S { static size_t maxsize; };
3) struct S { size_t maxsize; };
Applied to S, the code doesn't compile in 1 and returns false in 2.
Is there a fix that can make it work for 1, 2 and 3 ?
No. Static members and non-static ones are very different things, and each requires usage of a different macro. BOOST_HAS_STATIC_MEMBER_DATA and BOOST_HAS_MEMBER_DATA.
I can't find BOOST_HAS_STATIC_MEMBER_DATA in introspection in the sandbox. Were you referring to some header files elsewhere? Joel, re compiler, I am using vs2005. The case I really need is 1... so I need to detect the compile-time static constant, and not a variable member... so is there a way to do it? Rds,

-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of Mathias Gaunard Sent: 29 July 2009 13:12 To: boost-users@lists.boost.org Subject: Re: [Boost-users] detect if a type has a size_t member
I can't find BOOST_HAS_STATIC_MEMBER_DATA in introspection in the sandbox. Were you referring to some header files elsewhere?
Joel, re compiler, I am using vs2005.
The case I really need is 1... so I need to detect the compile-time static constant, and not a variable member... so is there a way to do it? Rds, ---------------- I just realized the current Boost.Introspection in sandbox is different from the version I downloaded weeks ago. I had (has_member_data.hpp has_member_function.hpp traits.hpp). Now I see there are different files (introspection.hpp detail.hpp detail/*) from https://svn.boost.org/svn/boost/sandbox/introspection/boost/introspection/ Nevertheless, I still couldn't figure out how to detect the static const size_t member. Which version to use? Rds,

Hicham Mouline wrote:
Nevertheless, I still couldn't figure out how to detect the static const size_t member.
Which version to use?
No version allows this at the moment. You may, however, try to simply duplicate HAS_STATIC_MEMBER (or whatever it's called), change &X::Name to X::Name, and Type* to Type.

-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of Mathias Gaunard Sent: 29 July 2009 15:48 To: boost-users@lists.boost.org Subject: Re: [Boost-users] detect if a type has a size_t member
Hicham Mouline wrote:
Nevertheless, I still couldn't figure out how to detect the static const size_t member.
Which version to use?
No version allows this at the moment. You may, however, try to simply duplicate HAS_STATIC_MEMBER (or whatever it's called), change &X::Name to X::Name, and Type* to Type.
Thanks very much, It worked for me on vs2005 and g++4.3.3 http://codepad.org/wsNi21Pr at least for the cases I wanted, that is struct S1 { static const size_t maxsize= 5; }; struct S5 { }; Nevertheless, it failed to compile on g++4.3.3 for struct S2 { static size_t maxsize; }; because of this error: error: 'S2::maxsize' is not a valid template argument for type 'long unsigned int' because it is a non-constant expression but compiled on vs2005 and failed ( which I don't care about ) thanks again,

Hicham Mouline wrote:
#include <boost/introspection/has_member_data.hpp>
struct S { static const size_t maxsize =5; };
struct T { };
BOOST_HAS_MEMBER_DATA(S, maxsize) BOOST_HAS_MEMBER_DATA(T, maxsize)
It's not BOOST_HAS_MEMBER_DATA you need, but BOOST_HAS_STATIC_MEMBER_NAMED. Indeed, BOOST_HAS_MEMBER_DATA is for non-static members.
participants (6)
-
Asif Lodhi
-
Hicham Mouline
-
joel
-
Mathias Gaunard
-
Roman Perepelitsa
-
Steven Watanabe