Template specialisation with multiple typenames

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Adam Nielsen

    Template specialisation with multiple typenames

    Hi everyone,

    I've having some trouble trying to specialise a template. My original
    code, with one typename, worked:

    template <typename T>
    class MyClass
    {
    private:
    T data;

    public:
    const std::string& toString() const
    {
    // convert this->data to a string and return
    }
    };

    const std::string& MyClass<std::st ring>::toString () const
    {
    return this->data; // don't bother with unnecessary conversion
    }

    However I then tried to extend the class with a second template
    parameter, and it stopped working:

    template <class C, typename T>
    class MyClass
    ...

    template <class C>
    const std::string& MyClass<C, std::string>::t oString() const
    {
    return this->data; // don't bother with unnecessary conversion
    }

    It would appear to work if I write code for every possible value of C,
    but then that's the whole reason for having a template! I'm assuming
    I've just gotten the syntax wrong, so if someone would be kind enough to
    point out the correct way of doing this it would be much appreciated!

    Many thanks,
    Adam.
  • Barry

    #2
    Re: Template specialisation with multiple typenames

    Adam Nielsen wrote:
    Hi everyone,
    >
    I've having some trouble trying to specialise a template. My original
    code, with one typename, worked:
    >
    template <typename T>
    class MyClass
    {
    private:
    T data;
    >
    public:
    const std::string& toString() const
    {
    // convert this->data to a string and return
    }
    };
    >
    const std::string& MyClass<std::st ring>::toString () const
    {
    return this->data; // don't bother with unnecessary conversion
    }
    >
    However I then tried to extend the class with a second template
    parameter, and it stopped working:
    >
    template <class C, typename T>
    class MyClass
    class MyClass<Type1>


    for example:

    template <class F = void()>
    class Func
    {
    };

    template <class Ret, class Arg>
    class Func<Ret(Arg)>
    ^^^^^^^^^^
    {
    };

    without "<Ret(Arg)> ", the compiler assumes that you're defining another
    *primary class template* with the same name, which violates the ODR.
    with "<Ret(Arg)> ", it's a partial specialization. Be aware the number if
    template parameters in the <must be same as the primary class template.

    You can't have a template class with different template arguments
    (default arguments also count). So you can't do it.
    ...
    >
    template <class C>
    const std::string& MyClass<C, std::string>::t oString() const
    {
    return this->data; // don't bother with unnecessary conversion
    }
    >
    It would appear to work if I write code for every possible value of C,
    but then that's the whole reason for having a template! I'm assuming
    I've just gotten the syntax wrong, so if someone would be kind enough to
    point out the correct way of doing this it would be much appreciated!
    >

    --
    Thanks
    Barry

    Comment

    • Adam Nielsen

      #3
      Re: Template specialisation with multiple typenames

      Hi Barry,

      Thanks for your reply!
      >However I then tried to extend the class with a second template
      >parameter, and it stopped working:
      >>
      > template <class C, typename T>
      > class MyClass
      >
      class MyClass<Type1>
      >
      >
      for example:
      >
      template <class F = void()>
      class Func
      {
      };
      >
      template <class Ret, class Arg>
      class Func<Ret(Arg)>
      ^^^^^^^^^^
      {
      };
      >
      without "<Ret(Arg)> ", the compiler assumes that you're defining another
      *primary class template* with the same name, which violates the ODR.
      with "<Ret(Arg)> ", it's a partial specialization. Be aware the number if
      template parameters in the <must be same as the primary class template.
      Sorry, I don't think I explained myself properly! I'm not after a
      second MyClass with different template parameters, I want to remove
      MyClass<typenam e Tand instead have MyClass<class C, typename T>.

      This works, but when it comes to defining a function in the class, it
      doesn't like my syntax:

      // Works with MyClass<typenam e T(specialisatio n when T=std::string)
      const std::string& MyClass<std::st ring>::toString () const
      {
      return this->data;
      }

      // Does not work with MyClass<class C, typename T>
      template <class C>
      const std::string& MyClass<C, std::string>::t oString() const
      {
      return this->data;
      }

      I would like the second function to be used for all instances where T =
      std::string, regardless of what C might be given as when constructing a
      MyClass object.
      >It would appear to work if I write code for every possible value of C,
      >but then that's the whole reason for having a template! I'm assuming
      >I've just gotten the syntax wrong, so if someone would be kind enough
      >to point out the correct way of doing this it would be much appreciated!
      >>
      >
      >
      Thanks,
      Adam.

      Comment

      • xtrigger303@gmail.com

        #4
        Re: Template specialisation with multiple typenames

        On Sep 26, 4:44 am, Adam Nielsen <adam.niel...@u q.edu.auwrote:
        Hi Barry,
        >
        Thanks for your reply!
        >
        >
        >
        However I then tried to extend the class with a second template
        parameter, and it stopped working:
        >
        template <class C, typename T>
        class MyClass
        >
        class MyClass<Type1>
        >
        for example:
        >
        template <class F = void()>
        class Func
        {
        };
        >
        template <class Ret, class Arg>
        class Func<Ret(Arg)>
        ^^^^^^^^^^
        {
        };
        >
        without "<Ret(Arg)> ", the compiler assumes that you're defining another
        *primary class template* with the same name, which violates the ODR.
        with "<Ret(Arg)> ", it's a partial specialization. Be aware the number if
        template parameters in the <must be same as the primary class template.
        >
        Sorry, I don't think I explained myself properly! I'm not after a
        second MyClass with different template parameters, I want to remove
        MyClass<typenam e Tand instead have MyClass<class C, typename T>.
        >
        This works, but when it comes to defining a function in the class, it
        doesn't like my syntax:
        >
        // Works with MyClass<typenam e T(specialisatio n when T=std::string)
        const std::string& MyClass<std::st ring>::toString () const
        {
        return this->data;
        }
        >
        // Does not work with MyClass<class C, typename T>
        template <class C>
        const std::string& MyClass<C, std::string>::t oString() const
        {
        return this->data;
        }
        >
        I would like the second function to be used for all instances where T =
        std::string, regardless of what C might be given as when constructing a
        MyClass object.
        >
        It would appear to work if I write code for every possible value of C,
        but then that's the whole reason for having a template! I'm assuming
        I've just gotten the syntax wrong, so if someone would be kind enough
        to point out the correct way of doing this it would be much appreciated!
        >
        Thanks,
        Adam.
        Hi,
        IMVHO in the case you have described you have to partially specialize
        the whole class.
        Which is something quite boring. You can you something like the
        following trick, obviosuly you have to tailor it to your needs... :-)
        Bye,
        Francesco

        #include <iostream>
        #include <string>

        template< typename T >
        class CTypeFromType
        {};

        template< typename T1, typename T2 >
        class A
        {
        public:

        std::string Do( void ) { return Do( CTypeFromType< T2 >() ); }

        private:

        template< typename T3 >
        std::string Do( CTypeFromType< T3 )
        { return "GENERAL"; }

        std::string Do( CTypeFromType< std::string )
        { return "SPECIALIZE D"; }
        };




        int main()
        {
        A< int, double obj1;
        std::cout << obj1.Do() << std::endl;

        A< int, std::string obj2;
        std::cout << obj2.Do() << std::endl;
        }

        Comment

        • Adam Nielsen

          #5
          Re: Template specialisation with multiple typenames

          IMVHO in the case you have described you have to partially specialize
          the whole class.
          Which is something quite boring. You can you something like the
          following trick, obviosuly you have to tailor it to your needs... :-)
          That's quite an interesting trick, I never thought of doing something
          like that - very useful! There's probably a bit too much overhead in
          that for my intended use, but it's intriguing nonetheless.

          Thanks,
          Adam.

          Comment

          Working...