Strange behavior of template code

A

avasilev

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());

}
 
A

avasilev

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.
 
M

Marc

avasilev said:
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?
 
M

Melissa

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top