473,471 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Template type deduction issues

I am sure this is going to turn out to be a dumb question but here goes
anyway.
I was trying out the Hello World equivalent of a template program that
determines whether the type passed is a class type. This example is
lifted straight out of C++ Templates by Josuttis et al. However I
can't get it to compile with VC++ 2005. Here is what I tried:

template<typename T>
class IsClassT
{
private:
typedef char One;
typedef struct { char a[2]; } Two;
template<typename Cstatic One test(int C::*);
template<typename Cstatic Two test(...);
public:
enum { Yes = sizeof(IsClassT<T>::test<T>(0)) == 1 };
enum { No = !Yes };
};

struct Idiot
{
};

int main()
{
if (IsClassT<Idiot>::Yes)
std::cout << "Hurray!\n";

return 0;
}

The compiler complains:

1>c:\work\workarea\allpurposeconsoleapp\allpurpose consoleapp\allpurposeconsoleapp.cpp(510)
: error C2783: 'IsClassT<T>::Two IsClassT<T>::test(...)' : could not
deduce template argument for 'C'
1 with
1 [
1 T=Idiot
1 ]
1>
c:\work\workarea\allpurposeconsoleapp\allpurposeco nsoleapp\allpurposeconsoleapp.cpp(508)
: see declaration of 'IsClassT<T>::test'
1 with
1 [
1 T=Idiot
1 ]
1>
c:\work\workarea\allpurposeconsoleapp\allpurposeco nsoleapp\allpurposeconsoleapp.cpp(520)
: see reference to class template instantiation 'IsClassT<T>' being
compiled
1 with
1 [
1 T=Idiot
1 ]
1>c:\work\workarea\allpurposeconsoleapp\allpurpose consoleapp\allpurposeconsoleapp.cpp(510)
: error C2784: 'IsClassT<T>::One IsClassT<T>::test(int C::* )' : could
not deduce template argument for 'int C::* ' from 'int'
1 with
1 [
1 T=Idiot
1 ]
1>
c:\work\workarea\allpurposeconsoleapp\allpurposeco nsoleapp\allpurposeconsoleapp.cpp(507)
: see declaration of 'IsClassT<T>::test'
1 with
1 [
1 T=Idiot
1 ]
1>c:\work\workarea\allpurposeconsoleapp\allpurpose consoleapp\allpurposeconsoleapp.cpp(510)
: error C2056: illegal expression

I thought promoting a zero integral constant to a pointer is preferred
over ellipsis?

thanks!

Aug 17 '06 #1
3 1593
Dilip wrote:
I am sure this is going to turn out to be a dumb question but here
goes anyway.

[...example of VC++ 2005 failing snipped...]

I thought promoting a zero integral constant to a pointer is preferred
over ellipsis?
It is most likely a bug in VC++. Have you tried asking in the VC++ NG
(microsoft.public.vc.language), maybe it's already known?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 17 '06 #2

Dilip wrote:
I am sure this is going to turn out to be a dumb question but here goes
anyway.
I was trying out the Hello World equivalent of a template program that
determines whether the type passed is a class type. This example is
lifted straight out of C++ Templates by Josuttis et al. However I
can't get it to compile with VC++ 2005. Here is what I tried:

template<typename T>
class IsClassT
{
private:
typedef char One;
typedef struct { char a[2]; } Two;
template<typename Cstatic One test(int C::*);
template<typename Cstatic Two test(...);
public:
enum { Yes = sizeof(IsClassT<T>::test<T>(0)) == 1 };
enum { No = !Yes };
};

struct Idiot
{
};

int main()
{
if (IsClassT<Idiot>::Yes)
std::cout << "Hurray!\n";

return 0;
}

The compiler complains:

1>c:\work\workarea\allpurposeconsoleapp\allpurpose consoleapp\allpurposeconsoleapp.cpp(510)
: error C2783: 'IsClassT<T>::Two IsClassT<T>::test(...)' : could not
deduce template argument for 'C'
1 with
1 [
1 T=Idiot
1 ]
1>
c:\work\workarea\allpurposeconsoleapp\allpurposeco nsoleapp\allpurposeconsoleapp.cpp(508)
: see declaration of 'IsClassT<T>::test'
1 with
1 [
1 T=Idiot
1 ]
1>
c:\work\workarea\allpurposeconsoleapp\allpurposeco nsoleapp\allpurposeconsoleapp.cpp(520)
: see reference to class template instantiation 'IsClassT<T>' being
compiled
1 with
1 [
1 T=Idiot
1 ]
1>c:\work\workarea\allpurposeconsoleapp\allpurpose consoleapp\allpurposeconsoleapp.cpp(510)
: error C2784: 'IsClassT<T>::One IsClassT<T>::test(int C::* )' : could
not deduce template argument for 'int C::* ' from 'int'
1 with
1 [
1 T=Idiot
1 ]
1>
c:\work\workarea\allpurposeconsoleapp\allpurposeco nsoleapp\allpurposeconsoleapp.cpp(507)
: see declaration of 'IsClassT<T>::test'
1 with
1 [
1 T=Idiot
1 ]
1>c:\work\workarea\allpurposeconsoleapp\allpurpose consoleapp\allpurposeconsoleapp.cpp(510)
: error C2056: illegal expression

I thought promoting a zero integral constant to a pointer is preferred
over ellipsis?

thanks!
I encountered a similar problem few weeks back. I was about to enter a
bug when I saw a bug already entered on similar lines.

http://connect.microsoft.com/VisualS...edbackID=99405

Aug 17 '06 #3

Dilip wrote:
I am sure this is going to turn out to be a dumb question but here goes
anyway.
I was trying out the Hello World equivalent of a template program that
determines whether the type passed is a class type. This example is
lifted straight out of C++ Templates by Josuttis et al. However I
can't get it to compile with VC++ 2005. Here is what I tried:

template<typename T>
class IsClassT
{
private:
typedef char One;
typedef struct { char a[2]; } Two;
template<typename Cstatic One test(int C::*);
template<typename Cstatic Two test(...);
public:
enum { Yes = sizeof(IsClassT<T>::test<T>(0)) == 1 };
enum { No = !Yes };
};

struct Idiot
{
};

int main()
{
if (IsClassT<Idiot>::Yes)
std::cout << "Hurray!\n";

return 0;
}

The compiler complains:

1>c:\work\workarea\allpurposeconsoleapp\allpurpose consoleapp\allpurposeconsoleapp.cpp(510)
: error C2783: 'IsClassT<T>::Two IsClassT<T>::test(...)' : could not
deduce template argument for 'C'
1 with
1 [
1 T=Idiot
1 ]
1>
c:\work\workarea\allpurposeconsoleapp\allpurposeco nsoleapp\allpurposeconsoleapp.cpp(508)
: see declaration of 'IsClassT<T>::test'
1 with
1 [
1 T=Idiot
1 ]
1>
c:\work\workarea\allpurposeconsoleapp\allpurposeco nsoleapp\allpurposeconsoleapp.cpp(520)
: see reference to class template instantiation 'IsClassT<T>' being
compiled
1 with
1 [
1 T=Idiot
1 ]
1>c:\work\workarea\allpurposeconsoleapp\allpurpose consoleapp\allpurposeconsoleapp.cpp(510)
: error C2784: 'IsClassT<T>::One IsClassT<T>::test(int C::* )' : could
not deduce template argument for 'int C::* ' from 'int'
1 with
1 [
1 T=Idiot
1 ]
1>
c:\work\workarea\allpurposeconsoleapp\allpurposeco nsoleapp\allpurposeconsoleapp.cpp(507)
: see declaration of 'IsClassT<T>::test'
1 with
1 [
1 T=Idiot
1 ]
1>c:\work\workarea\allpurposeconsoleapp\allpurpose consoleapp\allpurposeconsoleapp.cpp(510)
: error C2056: illegal expression

I thought promoting a zero integral constant to a pointer is preferred
over ellipsis?

thanks!
Of course, I got around the problem by making the member template test
to a non-template member function.

Aug 17 '06 #4

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

Similar topics

4
by: Dave | last post by:
Hello all, Consider this template: template <typename T> void foo(T bar) {...} Here are three ways to instantiate this: 1.
2
by: marco | last post by:
the problem: I use a typedef inside a class template, than I use this type (dim_v<N1>::Type) to define the argument of a template function f but when I call this function from main, the compiler...
4
by: Mat DeLong | last post by:
I have never been stuck on programming something before to the point I give up... this is a first. I am programming what should be something very easy in C++... using Templates. Here is the code,...
14
by: Bart Samwel | last post by:
Hi everybody, I would really like some help explaining this apparent discrepancy, because I really don't get it. Here is the snippet: void foo(int&); void foo(int const&); ...
4
by: George | last post by:
Dear All, I'm compiling the code below with IBM's xlC 6.0 and get the message, "rmspace.cpp", line 34.48: 1540-0298 (S) Template argument deduction cannot be performed using the function...
7
by: gretean | last post by:
I have a problem that's driving me crazy involving Microsoft's ability to deduce template parameters. I am using Visual Studio .NET (aka VC7?), and it gives an error compiling the following code....
0
by: Fei Liu | last post by:
Hello, We all know that a template function can automatically deduce its parameter type and instantiate, e.g. template <tpyename T> void func(T a); func(0.f); This will cause func<floatto...
3
by: Fei Liu | last post by:
Hello, We all know that a template function can automatically deduce its parameter type and instantiate, e.g. template <tpyename T> void func(T a); func(0.f); This will cause func<floatto...
5
by: C++Liliput | last post by:
Consider the following code template<class T1, class T2> T2 sum(T1 a, T1 b) { T2 ret = a + b; return ret; } int main()
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
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...
1
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.