how to do partial specialization template for non member method template

Discussion in 'C++' started by vj, Dec 20, 2010.

  1. vj

    vj Guest

    Hi All,

    I was working on a dummy routine to fill up an array with random
    numbers using function templates but jst hit a road block. I jst cant
    get the code below to compile with MSVC 2010 compiler. I m not sure if
    i m using the correct template specialization syntax here. So please
    provide me some insight as to what is wrong here:

    <code>
    template <int * arr, int size> void randfill()
    {
    arr[size]=rand();
    randfill<arr,size-1>();
    }

    template<int * arr> void randfill<arr,0>()
    {
    return;
    }//<!-- error is here

    //template instantiation code
    int arr[10];
    randfill<arr,10>();
    </code>

    the compilation error i m getting is stl.cpp(35) : error C2768:
    'randfill' : illegal use of explicit template arguments.

    Thanks in advance,
    VJ
    vj, Dec 20, 2010
    #1
    1. Advertising

  2. vj

    Marc Guest

    vj wrote:

    > I was working on a dummy routine to fill up an array with random
    > numbers using function templates but jst hit a road block. I jst cant
    > get the code below to compile with MSVC 2010 compiler. I m not sure if
    > i m using the correct template specialization syntax here. So please
    > provide me some insight as to what is wrong here:
    >
    > <code>
    > template <int * arr, int size> void randfill()
    > {
    > arr[size]=rand();
    > randfill<arr,size-1>();
    > }
    >
    > template<int * arr> void randfill<arr,0>()
    > {
    > return;
    > }//<!-- error is here
    >
    > //template instantiation code
    > int arr[10];
    > randfill<arr,10>();
    > </code>
    >
    > the compilation error i m getting is stl.cpp(35) : error C2768:
    > 'randfill' : illegal use of explicit template arguments.


    Your indices are off by 1.

    Why do you want to make arr a template parameter instead of a regular
    function argument?

    Functions don't have partial specialization, only full specialization
    and overloading. One common workaround is to use a class instead.
    Another one is to pass you randfill function an argument of type
    integral_constant<int,size> and overload.
    Marc, Dec 20, 2010
    #2
    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. Petre Iantu
    Replies:
    1
    Views:
    348
    Petre Iantu
    Aug 17, 2003
  2. Levent
    Replies:
    5
    Views:
    11,130
    Victor Bazarov
    May 10, 2005
  3. Kai-Uwe Bux
    Replies:
    1
    Views:
    470
  4. Joseph Turian
    Replies:
    2
    Views:
    459
  5. puzzlecracker
    Replies:
    2
    Views:
    283
    James Kanze
    Jan 29, 2009
Loading...

Share This Page