473,405 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

"has member" detection using templates - why "." operator fails ?


I'm hoping someone can tell me why using member address of works and why
using the dot operator does not in the code below.

The code below uses the template function resolution mechanism to
determine wether a class contains a member. My understanding is that if
a template function has an error during resolution of the function
types, it is quietly eliminated from the resolution process. This can
be used to detect things that would normally be an error to detect in
regular code, namely checking for existance of a member. To get this
mechanism to work, you need to contrive a class where you force the
compiler at compile time to resolve which function to use but never
actually call the function - sizeof comes to the aid. Each of the
functions used can return a different sized object and the selection can
proceed based on the size of the object that would be returned.

So far so good.

In my last attempt to do this, I noticed that no matter what I did, when
I attempted to use the "." operator, the error was not silent. GCC as
well as Comeau's try it out were not silent about function template
resolution error when a "." operator was involved. However, taking the
sizeof a pointer to member works as expected.

The code is below. As posted it compiles under GCC (and I suspect
comeau). I have not tested this on MSVC but if my experience a while
back is any indication, it probably does not compile indicating a bug in
MSVC.

Why does the "." operator fail to compile in this case and the &T::
succeed ?
struct NoMemb { char a[1]; };
struct Memb_MemberA { char a[2]; };
struct Memb_MemberB { char a[3]; };

template <typename T>
struct InitObject
{
private:

struct xA {};
struct xB : xA {};

template <int w_size>
struct Detect
{
};

public:

// detect using sizeof( &T::membername )
template <typename U>
inline static Memb_MemberB ObjInitSel(
U & obj, xB * b, Detect< sizeof(&U::MemberB) * = 0
);

template <typename U>
inline static Memb_MemberA ObjInitSel(
U & obj, xB * b, Detect< sizeof(&U::MemberA) * = 0
);

template <typename U>
inline static NoMemb ObjInitSel( U & obj, xA * a );
// detect using sizeof( T().membername )
template <typename U>
inline static Memb_MemberB ObjInitSelDot(
U & obj, xB * b, Detect< sizeof(U().MemberB) * = 0
);

template <typename U>
inline static Memb_MemberA ObjInitSelDot(
U & obj, xB * b, Detect< sizeof(U().MemberA) * = 0
);

template <typename U>
inline static NoMemb ObjInitSelDot( U & obj, xA * a );
inline int ObjTest()
{
typedef xB * bp;

T obj = T();

int a = sizeof( ObjInitSel( obj, bp() ) );

// uncomment line below to show the error using "."
// a += sizeof( ObjInitSelDot( obj, bp() ) );

return a;
}
};

/////////// test code

struct A
{
int a;
char x[10];
};
struct B
{
int MemberA;
char x[15];
};
struct C
{
int MemberB;
char x[22];
};

int main()
{
InitObject<A>().ObjTest();
InitObject<B>().ObjTest();
InitObject<C>().ObjTest();
}
Apr 9 '07 #1
5 2562
* Gianni Mariani:
>
I'm hoping someone can tell me why using member address of works and why
using the dot operator does not in the code below.

The code below uses the template function resolution mechanism to
determine wether a class contains a member. My understanding is that if
a template function has an error during resolution of the function
types, it is quietly eliminated from the resolution process. This can
be used to detect things that would normally be an error to detect in
regular code, namely checking for existance of a member. To get this
mechanism to work, you need to contrive a class where you force the
compiler at compile time to resolve which function to use but never
actually call the function - sizeof comes to the aid. Each of the
functions used can return a different sized object and the selection can
proceed based on the size of the object that would be returned.

So far so good.

In my last attempt to do this, I noticed that no matter what I did, when
I attempted to use the "." operator, the error was not silent. GCC as
well as Comeau's try it out were not silent about function template
resolution error when a "." operator was involved. However, taking the
sizeof a pointer to member works as expected.

The code is below. As posted it compiles under GCC (and I suspect
comeau). I have not tested this on MSVC but if my experience a while
back is any indication, it probably does not compile indicating a bug in
MSVC.
With MSVC 7.1 moving the Detect structure to global scope helps a lot,
but not for the ".".

Why does the "." operator fail to compile in this case and the &T::
succeed ?
Compiles fine with Comeau Online. ;-)

However, not with g++ 3.4.4 under Windows XP, and furthermore, changing
"U().Method" to "((U*)0)->Method" ICEs the compiler:

vc_project.cpp: In instantiation of `InitObject<A>':
vc_project.cpp:91: instantiated from here
vc_project.cpp:39: internal compiler error: Segmentation fault
Please submit a full bug report,
with preprocessed source if appropriate.
See <URL:http://www.mingw.org/bugs.shtmlfor instructions.

I suggest you submit a full bug report.

[code, snipped]

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Apr 9 '07 #2
Alf P. Steinbach wrote:
....
Compiles fine with Comeau Online. ;-)

However, not with g++ 3.4.4 under Windows XP, and furthermore, changing
"U().Method" to "((U*)0)->Method" ICEs the compiler:

vc_project.cpp: In instantiation of `InitObject<A>':
vc_project.cpp:91: instantiated from here
vc_project.cpp:39: internal compiler error: Segmentation fault
Please submit a full bug report,
with preprocessed source if appropriate.
See <URL:http://www.mingw.org/bugs.shtmlfor instructions.

I suggest you submit a full bug report.
It does not ICE on g++ 4.1.1. (does ICE on g++ 4.0.0)

It does compile fine with comeau. It looks like it's a bug in gcc.

Apr 9 '07 #3
Gianni Mariani wrote:
....
It does not ICE on g++ 4.1.1. (does ICE on g++ 4.0.0)

It does compile fine with comeau. It looks like it's a bug in gcc.
GCC bug is here:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31512
Apr 9 '07 #4
Alf P. Steinbach wrote:
However, not with g++ 3.4.4 under Windows XP, and furthermore, changing
"U().Method" to "((U*)0)->Method" ICEs the compiler:
While I'm sure that's not what the GCC guys intended, an internal
compiler error is a valid response to invocation of undefined
behavior :-)
Apr 9 '07 #5
* Ron Natalie:
Alf P. Steinbach wrote:
>However, not with g++ 3.4.4 under Windows XP, and furthermore,
changing "U().Method" to "((U*)0)->Method" ICEs the compiler:
While I'm sure that's not what the GCC guys intended, an internal
compiler error is a valid response to invocation of undefined
behavior :-)
Well, it's within a sizeof. But I agree that a case can be made that
it's still UB. I've tried to make that case (didn't convince many).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Apr 9 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Me | last post by:
Hi, I would like learn from people with experience in C++, which of the following styles of way to construct "get/set" member functions would be the best in terms of usability, speed, et cetera. ...
4
by: Jian H. Li | last post by:
Hello, What's the essential differences between the two ways of "class::member" & "object.member"(or object_pointer->member)? class C{ public: void f() {} int i; };
71
by: Greg | last post by:
Is it your general opinion that C# is generally designed/intended/ready to replace C++? I know the answer is not black and white but please repond YES or NO (add any comments if you would like) ...
3
by: brianlum | last post by:
Hi, I was wondering if anyone knew of a built in Python function that will check if an item is a member of a list, i.e., if item i is a member of list l. I read of a function "in" but I can't...
2
by: Lionel B | last post by:
I have a function which takes a functor argument. I which to call it for a functor which is actually a class member; this works fine, using the mem_fun_ref and bind1st functions (see listing 1...
0
by: serge | last post by:
I have 2 SQL Enterprise Editions running SP2 and same collation. When i try to start database mirroring I get the following error message: "The remote copy of database "ABC" has not been...
28
by: Jess | last post by:
Hello, It is said that if I implement a "swap" member function, then it should never throw any exception. However, if I implement "swap" non- member function, then the restriction doesn't...
4
by: viki | last post by:
We have zillion of instances inf instance->m_member in the code. We are going introduce the 'accessors' Get() and Set() for m_member, and m_member going private (and renamed, too). We would like...
3
by: =?Utf-8?B?Sm9uIEU=?= | last post by:
I have an interface class with maybe eight functions, defined in one workspace and am defining a class in a second workspace that derives from this interface. Unfortunately only 7 of the 8...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.