Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Strange behavior of template code

Reply
Thread Tools

Strange behavior of template code

 
 
avasilev
Guest
Posts: n/a
 
      01-11-2012
Hi all,

In the code below, when the signified code is commented out, code
works as expected - Type matches the (first) template argument.

However, if I uncomment it, Type becomes 'char' instead of
std::string. This is the same with both MSVC and GCC. So it works when
I have specialization of StripTag for a tag template with one and two
arguments, but when I specialize it in exactly the same way for three
arguments, I get this strange behavior.

Anyone has any ideas?

Code follows:
#include <typeinfo>
#include <stdio.h>
#include <string>

template <typename T>
struct StripTag
{typedef T Type;};

template<typename T, template<typename T> class Tag >
struct StripTag<Tag<T> >
{ typedef typename StripTag<T>::Type Type; };

template<typename T, typename X, template<typename T, typename X>
class Tag >
struct StripTag<Tag<T,X> >
{ typedef typename StripTag<T>::Type Type; };

/*
//UNCOMMENT THIS AND RECOMPILE
template<typename T, typename X, typename Y, template<typename T,
typename X, typename Y> class Tag >
struct StripTag<Tag<T,X,Y> >
{ typedef typename StripTag<T>::Type Type; };
*/

template <class C>
struct Test
{
typedef C Type;
};

template <typename A, typename B>
struct Blah{};

int main()
{

printf("typeid of StripTag=\t%s\n",
typeid(StripTag<std::string>::Type).name());
printf("typeid of StripTag2=\t%s\n", typeid(StripTag<Blah<std::string,
bool> >::Type).name());
printf("typeid of Test=\t\t%s\n",
typeid(Test<std::string>::Type).name());
printf("typeid of std::string=\t%s\n", typeid(std::string).name());

}
 
Reply With Quote
 
 
 
 
avasilev
Guest
Posts: n/a
 
      01-11-2012
On Jan 11, 8:54*pm, avasilev <(E-Mail Removed)> wrote:
> Hi all,
>
> In the code below, when the signified code is commented out, code
> works as expected - Type matches the (first) template argument.
>
> However, if I uncomment it, Type becomes 'char' instead of
> std::string. This is the same with both MSVC and GCC. So it works when
> I have specialization of StripTag for a tag template with one and two
> arguments, but when I specialize it in exactly the same way for three
> arguments, I get this strange behavior.
>
> Anyone has any ideas?
>
> Code follows:
> #include <typeinfo>
> #include <stdio.h>
> #include <string>
>
> template <typename T>
> struct StripTag
> {typedef T Type;};
>
> template<typename T, template<typename T> class Tag >
> struct StripTag<Tag<T> >
> { typedef typename StripTag<T>::Type Type; };
>
> template<typename T, typename X, template<typename T, typename X>
> class Tag >
> struct StripTag<Tag<T,X> >
> { typedef typename StripTag<T>::Type Type; };
>
> /*
> //UNCOMMENT THIS AND RECOMPILE
> template<typename T, typename X, typename Y, template<typename T,
> typename X, typename Y> class Tag >
> struct StripTag<Tag<T,X,Y> >
> { typedef typename StripTag<T>::Type Type; };
> */
>
> template <class C>
> struct Test
> {
> * * * * typedef C Type;
>
> };
>
> template <typename A, typename B>
> struct Blah{};
>
> int main()
> {
>
> printf("typeid of StripTag=\t%s\n",
> typeid(StripTag<std::string>::Type).name());
> printf("typeid of StripTag2=\t%s\n", typeid(StripTag<Blah<std::string,
> bool> >::Type).name());
> printf("typeid of Test=\t\t%s\n",
> typeid(Test<std::string>::Type).name());
> printf("typeid of std::string=\t%s\n", typeid(std::string).name());
>
>
>
>
>
>
>
> }


Ok answer found - std::string is itself a template, so it matches the
3-argument specialization.
 
Reply With Quote
 
 
 
 
Marc
Guest
Posts: n/a
 
      01-11-2012
avasilev wrote:

> Hi all,
>
> In the code below, when the signified code is commented out, code
> works as expected - Type matches the (first) template argument.
>
> However, if I uncomment it, Type becomes 'char' instead of
> std::string. This is the same with both MSVC and GCC. So it works when
> I have specialization of StripTag for a tag template with one and two
> arguments, but when I specialize it in exactly the same way for three
> arguments, I get this strange behavior.
>
> Anyone has any ideas?


What is std::string? It is a typedef for:
std::basic_string<char,std::char_traits<char>,std: :allocator<char>>

Does that help?
 
Reply With Quote
 
Melissa
Guest
Posts: n/a
 
      01-11-2012
On Wed, 11 Jan 2012 10:54:49 -0800 (PST)
avasilev <(E-Mail Removed)> wrote:

> Hi all,
>
> In the code below, when the signified code is commented out, code
> works as expected - Type matches the (first) template argument.
>
> However, if I uncomment it, Type becomes 'char' instead of
> std::string. This is the same with both MSVC and GCC. So it works when
> I have specialization of StripTag for a tag template with one and two
> arguments, but when I specialize it in exactly the same way for three
> arguments, I get this strange behavior.
>
> Anyone has any ideas?


string is template class with three parameters. With specialization
for three parameters you got strings first template parameter ,
which is char.



 
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
r H2 deduce deduce template argument of a template class inheritingfrom a non template base? nguillot C++ 5 03-08-2009 05:56 PM
How to use the template member function of a template in the memberfunction of another template class? Peng Yu C++ 3 10-26-2008 03:51 PM
template template arguments: expected a class template, got `Component<T1, T2, T3> gary.bernstein@gmail.com C++ 1 06-08-2007 07:10 AM
Re: A Newbie Question about template template template tom_usenet C++ 0 07-24-2003 12:06 PM
Re: A Newbie Question about template template template Chris Theis C++ 2 07-24-2003 09:42 AM



Advertisments