C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Problem building an example from C++ templates book.

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Andrew Maclean
Guest





PostPosted: Wed Apr 28, 2004 7:30 pm    Post subject: Problem building an example from C++ templates book. Reply with quote



OK I tried the microsoft.public.vc newsgroup and got no answer! So it looks
as if it is time to call in the experts! I really am stumped with this one
so any comments or help is appreciated.

In looking at the examplesat : http://www.josuttis.com/tmplbook/index.html
in particular, types/types/cpp and types/typet.hpp.
Has anyone been able to get it to compile for VC7.1?

The problem seems to lie in typet.hpp, namely the function:

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

The commpiler returns two errors:
c:BooksC++ Templatestypestypet.hpp(74): error C2783:
'IsFunctionT<T>::One IsFunctionT<T>::test(...)' : could not deduce template
argument for 'U'
with
[
T=int
]
and
c:BooksC++ Templatestypestypet.hpp(74): error C2784:
'IsFunctionT<T>::Two IsFunctionT<T>::test(U (*)[1])' : could not deduce
template argument for 'T1 (*)[1]' from 'int'
with
[
T=int
]


Note that to get this far you have to comment out the line contining the
macro:
MK_FUNDA_TYPE(wchar_t)
because wchar_t conflicts with unsigned short.


Interestingly the code built OK with GCC-3.1.0 however to build with
GCC-3.4.0 you have to change the line:
enum { Yes = sizeof(IsFunctionT<T>::test<T>(0)) == 1 };
to:
enum { Yes = sizeof(IsFunctionT<T>::template test<T>(0)) == 1 };
Which is fine.

So how do I help the compiler deduce the argument templates?

Please post any comments or help that you can give to the newsgroup.


Thanks
Andrew




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Sumit Rajan
Guest





PostPosted: Thu Apr 29, 2004 10:45 pm    Post subject: Re: Problem building an example from C++ templates book. Reply with quote




"Andrew Maclean" <itsme (AT) gok (DOT) com> wrote

Quote:
OK I tried the microsoft.public.vc newsgroup and got no answer! So it
looks
as if it is time to call in the experts! I really am stumped with this one
so any comments or help is appreciated.

In looking at the examplesat : http://www.josuttis.com/tmplbook/index.html
in particular, types/types/cpp and types/typet.hpp.
Has anyone been able to get it to compile for VC7.1?


I had problems compiling types.cpp (which #includes typet.hpp) with Borland
C++ and g++ 3.2, too. Comeau C++ 4.3.3 managed to compile it without
choking, though.

Regards,
Sumit.
--
Sumit Rajan <sumitrajan (AT) alexandria (DOT) cc>



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
kostya
Guest





PostPosted: Sun Feb 20, 2005 5:25 pm    Post subject: Re: Problem building an example from C++ templates book. Reply with quote



To compile with VC7.1 try that:
replace
template<typename T>
class IsFunctionT {
private:
typedef char One;
typedef struct { char a[2]; } Two;
template<typename U> static One test(...);
template<typename U> static Two test(U (*)[1]);
public:
enum { Yes = sizeof(IsFunctionT<T>::test<T>(0)) == 1 };
enum { No = !Yes };
};

with

typedef char One;
typedef struct { char a[2]; } Two;
template<typename U> One test(...);
template<typename U> Two test(U (*)[1]);

template<typename T>
class IsFunctionT {
public:
enum { Yes = sizeof(test<T>(0)) == 1 };
enum { No = !Yes };
};



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
White Wolf
Guest





PostPosted: Wed Feb 23, 2005 10:36 pm    Post subject: Re: Problem building an example from C++ templates book. Reply with quote

kostya wrote:
Quote:
enum { Yes = sizeof(IsFunctionT<T>::test<T>(0)) == 1 };

enum { Yes = sizeof(IsFunctionT<T>::template test<T>(0)) == 1 };

My guess.

--
WW aka Attila
:::
Only dead fish go with the flow.



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.