 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Zoltan cad Juhasz Guest
|
Posted: Wed Aug 17, 2005 9:06 am Post subject: Question about Dependent Name checking (with Templates) |
|
|
Hi!
The question is on checking (in a class) if a dependent name exists or
not, _without compile time error_, gaining a compile time boolean
value as a result.
The idea was based on the solution developed by Zoltan Porkolab and
Istvan Zolyomi
([url]http://gsd.web.elte.hu/Publications/GPCE_04/introspection.pdf)[/url].
Let me show you the code (with some comment - I will tell you more, if
something does not clear enough):
http://extraball.sunsite.dk/notepad.php?ID=8805&parse=c
This is intended to be the "metaprogram", which can check the existence
of a dependent (member) name. It clearly works for member data (with
member data pointers). But it for member functions it causes bad
answer, the 'Rescue' function is chosen all the time - even if the name
exists!
What may cause this difference between data and function pointers? At
Inspector template class we are expecting a "non-type bool constant". I
think there must be an implicit conversion member data pointers to
bool, because you can compare a member pointer with a null value just
like normal pointers, so
int MyClass::*memptr = 0;
if (memptr == 0) { ... } // --- should be the same as
if (memptr) { ... }
This works with Intel 9.0, Intel 8.1, MSVC 7.1, Commo 4.3.3 BETA.
For some strange reasons, there is no implicit conversion member
function pointers to bool... are there? If not so, do you know any
other way to make this stuff work?
We have experimented with many other ideas, but this problem still
resisted all our attempts. Maybe an open mind from outside would help
us out. ;-)
Thank you for dealing my problem!
Regards,
Zoltan `cad` Juhasz
http://people.inf.elte.hu/cad
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Anthony Williams Guest
|
Posted: Wed Aug 17, 2005 12:44 pm Post subject: Re: Question about Dependent Name checking (with Templates) |
|
|
"Zoltan cad Juhasz" <cad (AT) force-x (DOT) hu> writes:
| Quote: | The question is on checking (in a class) if a dependent name exists or
not, _without compile time error_, gaining a compile time boolean
value as a result.
Let me show you the code (with some comment - I will tell you more, if
something does not clear enough):
http://extraball.sunsite.dk/notepad.php?ID=8805&parse=c
This is intended to be the "metaprogram", which can check the existence
of a dependent (member) name. It clearly works for member data (with
member data pointers). But it for member functions it causes bad
answer, the 'Rescue' function is chosen all the time - even if the name
exists!
From 14.3.2p5 of the standard:
|
"- For a non-type template-parameter of type pointer to member function, no
conversions apply. If the template-argument represents a set of overloaded
member functions, the matching member function is selected from the set
(13.4)."
"- For a non-type template-parameter of type pointer to data member,
qualification conversions (4.4) are applied."
This implies that a pointer to member function cannot be converted to bool in
this context --- if you try just a simple test, you will see the issue:
struct X
{
void f();
};
template<bool test>
struct Y
{};
Y<&X::f> y; // compile error
Actually, if you try the same test with a pointer-to-data-member, then you
will also get a compile error, so I am surprised your code works with
pointer-to-data-members.
You can detect member functions of a specific signature, by replacing your
bool type with an appropriate pointer-to-member type:
template<typename T,void (T::*pmf)()>
struct Z
{};
Z<X,&X::f> z; // OK
It is theoretically possible to use function overloading and SFINAE to detect
the existence of data members and member functions, but gcc 3.4.4 and MSVC 7.1
both barf at my attempts (gcc fails due to an internal error). Comeau-online
accepts my code, but it's not much practical use if it only compiles on one
compiler.
Anthony
--
Anthony Williams
Software Developer
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Anthony Williams Guest
|
Posted: Wed Aug 17, 2005 11:16 pm Post subject: Re: Question about Dependent Name checking (with Templates) |
|
|
Anthony Williams <anthony_w.geo (AT) yahoo (DOT) com> writes:
| Quote: | "Zoltan cad Juhasz" <cad (AT) force-x (DOT) hu> writes:
The question is on checking (in a class) if a dependent name exists or
not, _without compile time error_, gaining a compile time boolean
value as a result.
Let me show you the code (with some comment - I will tell you more, if
something does not clear enough):
http://extraball.sunsite.dk/notepad.php?ID=8805&parse=c
This is intended to be the "metaprogram", which can check the existence
of a dependent (member) name. It clearly works for member data (with
member data pointers). But it for member functions it causes bad
answer, the 'Rescue' function is chosen all the time - even if the name
exists!
It is theoretically possible to use function overloading and SFINAE to detect
the existence of data members and member functions, but gcc 3.4.4 and MSVC 7.1
both barf at my attempts (gcc fails due to an internal error). Comeau-online
accepts my code, but it's not much practical use if it only compiles on one
compiler.
|
Well, g++ 4.0.1 compiles my code fine, so that makes two compilers. Here it
is:
// check_member_type, which can only be instantiated with
// pointers-to-members
template<typename T,typename U>
char check_member_type(U (T::*t));
// Mapper takes the size of the return from check_member_type
// and maps it back to a real type
template<unsigned sz>
struct Mapper
{
typedef char Result;
};
// overload of check that is used if check_member_type can be instantiated
// this implies that we have a member named test
// if there is no member named test, then this overload is eliminated through
SFINAE
template<typename T>
typename Mapper<sizeof(check_member_type(&T::test))>::Result
check(T*);
// catch-all overload
short check(...);
#include <iostream>
// sample structures with various types of member
struct X
{
void test();
};
struct Y
{
int test;
};
struct Z
{};
struct Z2
{
int test(int);
};
int main()
{
X x;
Y y;
Z z;
Z2 z2;
// X has a function member, so should print sizeof(char)
// Y has a data member, so should print sizeof(char)
// Z has neither, so should print sizeof(short)
// Z2 has a function member, so should print sizeof(char)
std::cout<<"X: "<
std::cout<<"Y: "<
std::cout<<"Z: "<
std::cout<<"Z2: "<
}
Anthony
--
Anthony Williams
Software Developer
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Zoltan cad Juhasz Guest
|
Posted: Wed Aug 17, 2005 11:17 pm Post subject: Re: Question about Dependent Name checking (with Templates) |
|
|
" This implies that a pointer to member function cannot be converted to
bool in
this context --- if you try just a simple test, you will see the issue:
"
struct X
{
void f();
};
template<bool test>
struct Y
{};
Y<&X::f> y; // compile error
It is strange, but there is no compiler error with Intel 9.0, but there
is with MSVC 7.1 (just for pointer-to-member-function). Hmm?
"You can detect member functions of a specific signature, by replacing
your
bool type with an appropriate pointer-to-member type: "
Yeah, that is true. But we would like to check just a name without
signature and type etc. :(
"Comeau-online
accepts my code, but it's not much practical use if it only compiles on
one
compiler."
Agree! Thank you very much Anthony. We will try to find another way!
Regards,
Zoltan `cad` Juhasz
http://people.inf.elte.hu/cad
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Zoltan cad Juhasz Guest
|
Posted: Fri Aug 19, 2005 2:31 pm Post subject: Re: Question about Dependent Name checking (with Templates) |
|
|
" Well, g++ 4.0.1 compiles my code fine, so that makes two compilers.
Here it
is: "
(...)
Hmm, very nice idea! It is quite interesting, Intel 9.0 and Commo
compiles your code fine too, but the name checking implies bad answear
(always says: name does not exist, even if it does Hmm?
X: 2
Y: 2
Z: 2
Z2: 2
Is the name checking fine with g++ 4.0.1?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Anthony Williams Guest
|
Posted: Fri Aug 19, 2005 3:36 pm Post subject: Re: Question about Dependent Name checking (with Templates) |
|
|
"Zoltan cad Juhasz" <cad (AT) force-x (DOT) hu> writes:
| Quote: | " Well, g++ 4.0.1 compiles my code fine, so that makes two compilers.
Here it
is: "
(...)
Hmm, very nice idea! It is quite interesting, Intel 9.0 and Commo
compiles your code fine too, but the name checking implies bad answear
(always says: name does not exist, even if it does Hmm?
X: 2
Y: 2
Z: 2
Z2: 2
Is the name checking fine with g++ 4.0.1?
|
Yes; I wouldn't have posted the code otherwise.
Anthony
--
Anthony Williams
Software Developer
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|