Problem on template specialization

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

    #1

    Problem on template specialization

    Dear All,

    I have a simple code, but it has some problem in cimpiling. Would you
    please help me to look at it?

    template<class Type, size_t N>
    class Vector
    {
    private:
    Type data[N];

    public:

    template<size_t i>
    Type Product() const
    {
    return data[i] * Product(i+1);
    }

    template<>
    Type Product<N>() const
    {
    return 1;
    }
    }

    The compilor error is that:

    Error 1 error C2975: 'i' : invalid template argument for
    'Vector<Type,N> ::Product', expected compile-time constant expression

    What I assume is that N is a compile-time constant expression. I
    appreciate your help.

    Bests,

    Shuisheng

  • Rolf Magnus

    #2
    Re: Problem on template specialization

    shuisheng wrote:
    Dear All,
    >
    I have a simple code, but it has some problem in cimpiling. Would you
    please help me to look at it?
    >
    template<class Type, size_t N>
    class Vector
    {
    private:
    Type data[N];
    >
    public:
    >
    template<size_t i>
    Type Product() const
    {
    return data[i] * Product(i+1);
    There is no Product() function that takes an integer as argument in your
    code.
    }
    >
    template<>
    Type Product<N>() const
    {
    return 1;
    }
    }
    >
    The compilor error is that:
    >
    Error 1 error C2975: 'i' : invalid template argument for
    'Vector<Type,N> ::Product', expected compile-time constant expression
    Which line does that error refer to?
    What I assume is that N is a compile-time constant expression.
    Well, you haven't showed how you are instantiating your template.

    Comment

    • shuisheng

      #3
      Re: Problem on template specialization


      Rolf Magnus 写道:
      template<class Type, size_t N>
      class Vector
      {
      private:
      Type data[N];

      public:

      template<size_t i>
      Type Product() const
      {
      return data[i] * Product(i+1);
      >
      There is no Product() function that takes an integer as argument in your
      code.
      >
      }

      template<>
      Type Product<N>() const <<<<<<<<<<< ERROR LINE

      {
      return 1;
      }
      }

      The compilor error is that:

      Error 1 error C2975: 'i' : invalid template argument for
      'Vector<Type,N> ::Product', expected compile-time constant expression
      >
      Which line does that error refer to?
      >
      What I assume is that N is a compile-time constant expression.
      >
      Well, you haven't showed how you are instantiating your template.
      Vector<int, 4v(1);
      int prod = v.Product<0>();

      Thanks.

      Comment

      • Jeff Faust

        #4
        Re: Problem on template specialization

        You can't specialize a class function on the class template parameter.
        You can't use 'N' as the specialization. I can dig up the standard-eze
        if you'd like.

        Jeff

        Comment

        Working...