473,396 Members | 2,129 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,396 software developers and data experts.

Types ambiguity

Hello

Consider the following program:
#include <iostream>
#include <string>

class TestClass
{
public:
void f(void) {}
};

template<typename T>
struct F{
typedef void (T::*Type)(void);
};

template<typename T>
void Execute_generic(T) {
std::cout << "Generic function" << std::endl;
}

template<typename T>
void Execute_generic(typename F<T>::Type) {
//void Execute_generic( void (T::*)(void) ) {
std::cout << "Specialization {void (T::*)(void)}" << std::endl;
}

template<typename T>
void Execute_generic(T*) {
std::cout << "Specialization {T*}" << std::endl;
}

template<typename T>
class C {
public:
void fun(T t) { Execute_generic(t); }
};
int main(void)
{
C<std::stringc1;
typedef void (TestClass::*pmem)(void);
C<pmemc2;
C<double*c3;
double u=0.0;

c1.fun(std::string("Hello"));
c2.fun(&TestClass::f);
c3.fun(&u);

return 0;
}
When I uncomment the line:
void Execute_generic( void (T::*)(void) )
I got output:

Generic function
Specialization {void (T::*)(void)}
Specialization {T*}

which is what I'm trying to achieve. But when I uncomment the line
that says:
void Execute_generic(typename F<T>::Type)
I got output:
Generic function
Generic function
Specialization {T*}

which I don't understand. What's the difference between void (T::*)
(void) and typename F<T>::Type.
It seems to be the same.

Compiled in Comeau online, MVisual and GCC with pedantic without
warnings.

Thanks for your help
braton
Jan 2 '08 #1
5 1583
br****@gmail.com wrote:
When I uncomment the line:
void Execute_generic( void (T::*)(void) )
I got output:

Generic function
Specialization {void (T::*)(void)}
Specialization {T*}

which is what I'm trying to achieve. But when I uncomment the line
that says:
void Execute_generic(typename F<T>::Type)
I got output:
Generic function
Generic function
Specialization {T*}

which I don't understand. What's the difference between void (T::*)
(void) and typename F<T>::Type.
It seems to be the same.
There's nothing different between these two in effect, the former one is
a alias(typedef creates a type alias) of the second one.
Compiled in Comeau online, MVisual and GCC with pedantic without
warnings.
Your compiler is not standard conformed.
Jan 3 '08 #2
There's nothing different between these two in effect, the former one is
a alias(typedef creates a type alias) of the second one.

Your compiler is not standard conformed.

So according to standard C++ the result of the above-mentioned code
snipped should looks like this:

Generic function
Specialization {void (T::*)(void)}
Specialization {T*}

or there's a reason why I receive the other output when I use
"templated" typedef. It seems strange to me that both Visual C++ and
GCC are not standard conformed but I don't say it's impossible in this
case.

Regards
braton
Jan 3 '08 #3
On Jan 2, 5:46 pm, bra...@gmail.com wrote:
Consider the following program:

#include <iostream>
#include <string>

class TestClass
{
public:
void f(void) {}

};

template<typename T>
struct F{
typedef void (T::*Type)(void);

};

template<typename T>
void Execute_generic(T) {
std::cout << "Generic function" << std::endl;

}

template<typename T>
void Execute_generic(typename F<T>::Type) {
//void Execute_generic( void (T::*)(void) ) {
std::cout << "Specialization {void (T::*)(void)}" << std::endl;

}

template<typename T>
void Execute_generic(T*) {
std::cout << "Specialization {T*}" << std::endl;

}

template<typename T>
class C {
public:
void fun(T t) { Execute_generic(t); }

};

int main(void)
{
C<std::stringc1;
typedef void (TestClass::*pmem)(void);
C<pmemc2;
C<double*c3;
double u=0.0;

c1.fun(std::string("Hello"));
c2.fun(&TestClass::f);
c3.fun(&u);

return 0;

}

When I uncomment the line:
void Execute_generic( void (T::*)(void) )
I got output:
Generic function
Specialization {void (T::*)(void)}
Specialization {T*}
which is what I'm trying to achieve. But when I uncomment the line
that says:
void Execute_generic(typename F<T>::Type)
I got output:
Generic function
Generic function
Specialization {T*}
which I don't understand. What's the difference between void (T::*)
(void) and typename F<T>::Type.
It seems to be the same.
typename F<T>::Type is an non-deduced context. The compiler
cannot use it to deduce T. (There is a very simple technical
reason for this. What the compiler knows when it tries to do
type deduction here is the type it needs for F<T>::Type. The
only way it can find this type, however, is by generating all
possible instantiations of F<Tuntil it finds one with a Type
that matches. Since the number of possible instantiations is
infinite, the problem is unsolvable.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jan 3 '08 #4
On Jan 3, 8:48 am, bra...@gmail.com wrote:
There's nothing different between these two in effect, the
former one is a alias(typedef creates a type alias) of the
second one.
Your compiler is not standard conformed.
I disagree.
So according to standard C++ the result of the above-mentioned
code snipped should looks like this:
Generic function
Specialization {void (T::*)(void)}
Specialization {T*}
No. As you presented it, there is no way for the compiler to do
type deduction for the second case, so it ends up using the
more generic form.
or there's a reason why I receive the other output when I use
"templated" typedef. It seems strange to me that both Visual
C++ and GCC are not standard conformed but I don't say it's
impossible in this case.
I know a couple of cases where both VC++ and g++ have the same
bug, but this isn't one of them.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jan 3 '08 #5
typename F<T>::Type is an non-deduced context. The compiler
cannot use it to deduce T. (There is a very simple technical
reason for this. What the compiler knows when it tries to do
type deduction here is the type it needs for F<T>::Type. The
only way it can find this type, however, is by generating all
possible instantiations of F<Tuntil it finds one with a Type
that matches. Since the number of possible instantiations is
infinite, the problem is unsolvable.)
Thanks for clarification, I see it now.

Regards
braton
Jan 3 '08 #6

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

Similar topics

15
by: Terje Slettebø | last post by:
Hi. I'm new here, and sorry if this has been discussed before; I didn't find it searching the PHP groups. (I've also read recommendations to cross-post to the other PHP groups, but if that is...
8
by: Shailesh | last post by:
One problem I've been wrestling with for a long time is how to use the C++ integral data types, vis-a-vis their size. The C++ rules guarantee that a char is at least 1 bytes, a short and int at...
7
by: Shailesh | last post by:
One problem I've been wrestling with for a long time is how to use the C++ integral data types, vis-a-vis their size. The C++ rules guarantee that a char is at least 1 bytes, a short and int at...
4
by: abendstund | last post by:
Hi, I have the following code and trouble with ambiguity due to operator overloading.. The code is also at http://paste.nn-d.de/441 snip>>
7
by: abendstund | last post by:
Hi, I have the following code and trouble with ambiguity due to operator overloading.. The code is also at http://paste.nn-d.de/441 snip>>
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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.