specialize a template function that contains a template parameter

Discussion in 'C++' started by sebastian, Sep 17, 2005.

  1. sebastian

    sebastian Guest

    Hi,
    I'd like to specialize a template function that contains a template
    parameter. In Example i have the following function declared:

    ....

    template < int i > static stupid_object& doSomething( int i,
    const another_stupid_object& __aso);

    ....

    and now i would like to specialize this function for i equals 6.
    How can i realize this ?:

    ....
    template<> stupid_object& doSomething( ???? ) {
    ....
    }
    ....

    In the broader sens what happens if the template-argument is not of a
    simple type like "int" but an own declared object (i.E. "own_object").
    Which operators do i have to overwrite ? Is there any documentation about
    this ?
    Unfortunately Stroustrup doesn't write anything about this case. Is
    it generally realizable/implementable ???

    Thanx in advance
    cheers
    sebastian
    sebastian, Sep 17, 2005
    #1
    1. Advertising

  2. sebastian

    Greg Guest

    sebastian wrote:
    > Hi,
    > I'd like to specialize a template function that contains a template
    > parameter. In Example i have the following function declared:
    >
    > ...
    >
    > template < int i > static stupid_object& doSomething( int i,
    > const another_stupid_object& __aso);
    >
    > ...
    >
    > and now i would like to specialize this function for i equals 6.
    > How can i realize this ?:
    >
    > ...
    > template<> stupid_object& doSomething( ???? ) {
    > ...
    > }
    > ...
    >
    > In the broader sens what happens if the template-argument is not of a
    > simple type like "int" but an own declared object (i.E. "own_object").
    > Which operators do i have to overwrite ? Is there any documentation about
    > this ?
    > Unfortunately Stroustrup doesn't write anything about this case. Is
    > it generally realizable/implementable ???
    >
    > Thanx in advance
    > cheers
    > sebastian


    It is not possible for the compiler to deduce a non-type template
    parameter for a function template. To do so, the compiler would have to
    know which calls to that function passed the value 6, and also be sure
    that the value passed was always 6. Since it can do neither, the
    compiler requires that every call to doSomething specifies the non-type
    parameter:

    doSomething<6>( i, a);

    Of course, this syntax is no better than simply writing a function
    called doSomething6. And for that reason function templates are rarely
    declared with non-type parameters. They just are not very useful.

    But to answer the question, a function template with an int
    parameterized type can be specialized for the value 6 in this way:

    template <>
    int DoSomething<6>(int i, someType s)
    {
    ...

    Greg
    Greg, Sep 17, 2005
    #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. Old Wolf
    Replies:
    4
    Views:
    376
    Victor Bazarov
    Apr 8, 2005
  2. mrstephengross
    Replies:
    1
    Views:
    276
    Victor Bazarov
    Aug 2, 2005
  3. Replies:
    1
    Views:
    351
    Victor Bazarov
    Feb 17, 2006
  4. toton
    Replies:
    3
    Views:
    379
    Victor Bazarov
    Jan 25, 2007
  5. Replies:
    16
    Views:
    575
    Victor Bazarov
    Mar 15, 2007
Loading...

Share This Page