Problem on template definition

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

    #1

    Problem on template definition

    Dear All;

    Would you please help me to look at the following case:

    //! Rotation.
    enum Rotation {
    NON_CYCLIC,
    CYCLIC
    };
    //! Rotation.
    enum Direction {
    LEFT,
    RIGHT
    };

    //! This is a class template of TinyVector
    template<class _T, size_t _n>
    class TinyVector
    {
    public:
    //! Rotate.
    template<Direct ion _dir, Rotation _rot>
    TinyVector& Rotate(size_t n);
    }

    //! Rotate.
    template<class _T, size_t _n>
    template<Direct ion _dir, Rotation _rot>
    inline typename TinyVector<_T, _n>& TinyVector<_T, _n>::Rotate(siz e_t
    n)
    {
    ...
    return *this;
    }

    //! Rotate<LEFT, NON_CYCLIC>.
    template<class _T, size_t _n>
    template<>
    inline typename TinyVector<_T, _n>& TinyVector<_T, _n>::Rotate<LEF T,
    NON_CYCLIC>(siz e_t n)
    {
    ...
    return *this;
    } // <-- Error position

    Error C2768: 'TinyVector<_T, _n>::Rotate' : illegal use of explicit
    template arguments

    I realy appreciate your guys' help. It make me to learn c++ much more
    comforably.

    Shuisheng

  • shuisheng

    #2
    Re: Problem on template definition


    shuisheng wrote:
    Dear All;
    >
    Would you please help me to look at the following case:
    >
    //! Rotation.
    enum Rotation {
    NON_CYCLIC,
    CYCLIC
    };
    //! Rotation.
    enum Direction {
    LEFT,
    RIGHT
    };
    >
    //! This is a class template of TinyVector
    template<class _T, size_t _n>
    class TinyVector
    {
    public:
    //! Rotate.
    template<Direct ion _dir, Rotation _rot>
    TinyVector& Rotate(size_t n);
    }
    >
    //! Rotate.
    template<class _T, size_t _n>
    template<Direct ion _dir, Rotation _rot>
    inline typename TinyVector<_T, _n>& TinyVector<_T, _n>::Rotate(siz e_t
    n)
    {
    ...
    return *this;
    }
    >
    //! Rotate<LEFT, NON_CYCLIC>.
    template<class _T, size_t _n>
    template<>
    Reverse those two template declaration. It becomes okay. Quite
    interesting.

    template<>
    template<class _T, size_t _n>

    inline typename TinyVector<_T, _n>& TinyVector<_T, _n>::Rotate<LEF T,
    NON_CYCLIC>(siz e_t n)
    {
    ...
    return *this;
    } // <-- Error position
    >
    Error C2768: 'TinyVector<_T, _n>::Rotate' : illegal use of explicit
    template arguments
    >
    I realy appreciate your guys' help. It make me to learn c++ much more
    comforably.
    >
    Shuisheng

    Comment

    • shuisheng

      #3
      Re: Problem on template definition


      shuisheng wrote:
      shuisheng wrote:
      Dear All;

      Would you please help me to look at the following case:

      //! Rotation.
      enum Rotation {
      NON_CYCLIC,
      CYCLIC
      };
      //! Rotation.
      enum Direction {
      LEFT,
      RIGHT
      };

      //! This is a class template of TinyVector
      template<class _T, size_t _n>
      class TinyVector
      {
      public:
      //! Rotate.
      template<Direct ion _dir, Rotation _rot>
      TinyVector& Rotate(size_t n);
      }

      //! Rotate.
      template<class _T, size_t _n>
      template<Direct ion _dir, Rotation _rot>
      inline typename TinyVector<_T, _n>& TinyVector<_T, _n>::Rotate(siz e_t
      n)
      {
      ...
      return *this;
      }

      //! Rotate<LEFT, NON_CYCLIC>.
      template<class _T, size_t _n>
      template<>
      >
      Reverse those two template declaration. It becomes okay. Quite
      interesting.
      >
      template<>
      template<class _T, size_t _n>
      >
      >
      inline typename TinyVector<_T, _n>& TinyVector<_T, _n>::Rotate<LEF T,
      NON_CYCLIC>(siz e_t n)
      {
      ...
      return *this;
      } // <-- Error position

      Error C2768: 'TinyVector<_T, _n>::Rotate' : illegal use of explicit
      template arguments

      I realy appreciate your guys' help. It make me to learn c++ much more
      comforably.

      Shuisheng
      Still WRONG. Help!!!

      Comment

      • amparikh@gmail.com

        #4
        Re: Problem on template definition


        shuisheng wrote:
        Dear All;
        >
        Would you please help me to look at the following case:
        >
        //! Rotation.
        enum Rotation {
        NON_CYCLIC,
        CYCLIC
        };
        //! Rotation.
        enum Direction {
        LEFT,
        RIGHT
        };
        >
        //! This is a class template of TinyVector
        template<class _T, size_t _n>
        class TinyVector
        {
        public:
        //! Rotate.
        template<Direct ion _dir, Rotation _rot>
        TinyVector& Rotate(size_t n);
        }
        >
        //! Rotate.
        template<class _T, size_t _n>
        template<Direct ion _dir, Rotation _rot>
        inline typename TinyVector<_T, _n>& TinyVector<_T, _n>::Rotate(siz e_t
        n)
        {
        ...
        return *this;
        }
        >
        //! Rotate<LEFT, NON_CYCLIC>.
        template<class _T, size_t _n>
        template<>
        inline typename TinyVector<_T, _n>& TinyVector<_T, _n>::Rotate<LEF T,
        NON_CYCLIC>(siz e_t n)
        {
        ...
        return *this;
        } // <-- Error position
        You cannot specialize a template member function without first
        specializing the class template.
        so you need to first specialize the class template for a specific type.
        >
        Error C2768: 'TinyVector<_T, _n>::Rotate' : illegal use of explicit
        template arguments
        >
        I realy appreciate your guys' help. It make me to learn c++ much more
        comforably.
        >
        Shuisheng

        Comment

        • Kai-Uwe Bux

          #5
          Re: Problem on template definition

          shuisheng wrote:
          //! Rotation.
          enum Rotation           {
          NON_CYCLIC,
          CYCLIC
          };
          //! Rotation.
          enum Direction          {
          LEFT,
          RIGHT
          };
          >
          //! This is a class template of TinyVector
          template<class _T, size_t _n>
          class TinyVector
          {
          public:
          //! Rotate.
          template<Direct ion _dir, Rotation _rot>
          TinyVector& Rotate(size_t n);
          }
          missing ";":
          };

          that one already causes a lot of confusing error messages to go away.
          >
          //! Rotate.
          template<class _T, size_t _n>
          template<Direct ion _dir, Rotation _rot>
          inline typename TinyVector<_T, _n>& TinyVector<_T, _n>::Rotate(siz e_t n)
          ditch the "typename"
          {
          ...
          return *this;
          }
          >
          //! Rotate<LEFT, NON_CYCLIC>.
          template<class _T, size_t _n>
          template<>
          inline typename TinyVector<_T, _n>& TinyVector<_T, _n>::Rotate<LEF T,
          NON_CYCLIC>(siz e_t n)
          {
          ...
          return *this;
          }   // <-- Error position
          >
          Finally, this last one cannot fly at all: as of the current standard,
          partial specializations of function templates do not exist in C++. You have
          to turn that into a class template. The class could be stateless and just
          provide a static function.


          You could try something like:

          #include <cstddef>

          typedef int Direction;
          typedef int Rotation;

          //! This is a class template of TinyVector
          template<class T, size_t N >
          class TinyVector;

          template < Direction dir, Rotation rot >
          struct alg;

          template<>
          struct alg< 0, 0 {

          template<class T, size_t N >
          static
          TinyVector<T,N& rotate ( TinyVector<T,N& arg ) {
          return ( arg );
          }

          }; // alg;


          template<class T, size_t N >
          class TinyVector {

          public:

          template<Direct ion dir, Rotation rot>
          TinyVector& Rotate(size_t n) {
          return ( alg<dir,rot>::r otate( *this ) );
          }

          };




          Best

          Kai-Uwe Bux

          Comment

          • Nate Barney

            #6
            Re: Problem on template definition

            shuisheng wrote:
            template<class _T, size_t _n>
            Identifiers which begin with an underscore are reserved for the
            implementation. Use T and n instead. If you really want to use an
            underscore, you can use T_ and n_ .

            Nate

            Comment

            • Victor Bazarov

              #7
              Re: Problem on template definition

              Nate Barney wrote:
              shuisheng wrote:
              >template<cla ss _T, size_t _n>
              >
              Identifiers which begin with an underscore are reserved for the
              implementation.
              Only in the global scope. Of course those that begin with a '_'
              and a capital letter (like "_T" here) are always reserved, no
              matter what scope.
              Use T and n instead. If you really want to use an
              underscore, you can use T_ and n_ .
              I think many who use the leading underscore think that with it
              their code looks cool because it looks more like the "internal
              stuff" or the "implementa tion detail", or some such. Or, maybe
              they think it would help avoid naming conflict somewhere down
              the road (which it actually doesn't).

              V
              --
              Please remove capital 'A's when replying by e-mail
              I do not respond to top-posted replies, please don't ask


              Comment

              • Nate Barney

                #8
                Re: Problem on template definition

                Victor Bazarov wrote:
                Nate Barney wrote:
                >Identifiers which begin with an underscore are reserved for the
                >implementation .
                >
                Only in the global scope. Of course those that begin with a '_'
                and a capital letter (like "_T" here) are always reserved, no
                matter what scope.
                Interesting, I didn't know that. In any case, it's probably best to
                eschew leading underscores entirely.

                Nate

                Comment

                Working...