Strange behavior of template code

Discussion in 'C++' started by avasilev, Jan 11, 2012.

  1. avasilev

    avasilev Guest

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

    }
    avasilev, Jan 11, 2012
    #1
    1. Advertising

  2. avasilev

    avasilev Guest

    On Jan 11, 8:54 pm, 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?
    >
    > 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.
    avasilev, Jan 11, 2012
    #2
    1. Advertising

  3. avasilev

    Marc Guest

    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?
    Marc, Jan 11, 2012
    #3
  4. avasilev

    Melissa Guest

    On Wed, 11 Jan 2012 10:54:49 -0800 (PST)
    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?


    string is template class with three parameters. With specialization
    for three parameters you got strings first template parameter ,
    which is char.
    Melissa, Jan 11, 2012
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Theis
    Replies:
    2
    Views:
    453
    Chris Theis
    Jul 24, 2003
  2. tom_usenet
    Replies:
    0
    Views:
    520
    tom_usenet
    Jul 24, 2003
  3. Harvey Twyman
    Replies:
    8
    Views:
    548
    August Derleth
    Oct 25, 2003
  4. Replies:
    1
    Views:
    2,082
    Gianni Mariani
    Jun 8, 2007
  5. Peng Yu
    Replies:
    3
    Views:
    754
    Thomas J. Gritzan
    Oct 26, 2008
Loading...

Share This Page