Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Template type deduction issues

Reply
Thread Tools

Template type deduction issues

 
 
Dilip
Guest
Posts: n/a
 
      08-17-2006
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 C> static One test(int C::*);
template<typename C> static 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(50
: 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!

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-17-2006
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


 
Reply With Quote
 
 
 
 
amparikh@gmail.com
Guest
Posts: n/a
 
      08-17-2006

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 C> static One test(int C::*);
> template<typename C> static 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(50
> : 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

 
Reply With Quote
 
amparikh@gmail.com
Guest
Posts: n/a
 
      08-17-2006

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 C> static One test(int C::*);
> template<typename C> static 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(50
> : 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
function template type deduction from default parameter tobias.loew@steag.com C++ 2 01-08-2013 07:53 AM
function pointer as template parameter + type deduction er C++ 5 06-09-2009 02:04 AM
return type deduction for a function template C++Liliput C++ 2 10-24-2008 08:09 AM
template class instantiate without template parameter, automatic type deduction Fei Liu C++ 4 10-26-2007 02:39 PM
template class instantiate without template parameter, automatictype deduction Fei Liu C++ 0 10-25-2007 08:12 PM



Advertisments