Template parameter Deduction

Discussion in 'C++' started by Neelesh, Nov 10, 2005.

  1. Neelesh

    Neelesh Guest

    Hi all,
    I had some confusion about deduction of non-type template parameters
    for function templates :

    template <class T, int i> void foo (T, int p = i) ;

    void bar()
    {
    foo<int, 10>(40,40); // OK, T = int, i = 10
    foo(40,40); // ERROR, T = int but i cannot be deduced.
    }

    However here I have another example :

    template <class T, int i> void lookup ( Buffer<T,i> b);

    void bar(Buffer<int, 128> b)
    {
    lookup(b,100); // No error, can infer T = int, i = 128
    }

    So it seems that when 'i' gets inferred as a "side effect" of type
    matching, the compiler accepts that. However, it doesnot try to infer
    it explicitly if required.

    What does the standard say about this?
     
    Neelesh, Nov 10, 2005
    #1
    1. Advertising

  2. Neelesh

    Greg Guest

    Neelesh wrote:
    > Hi all,
    > I had some confusion about deduction of non-type template parameters
    > for function templates :
    >
    > template <class T, int i> void foo (T, int p = i) ;
    >
    > void bar()
    > {
    > foo<int, 10>(40,40); // OK, T = int, i = 10
    > foo(40,40); // ERROR, T = int but i cannot be deduced.
    > }


    The compiler cannot deduce non-type parameters for function templates
    since there is no requirement that the parameters be compile-time
    constants. Moreover, non-type template parameters are not all that
    useful. Why call one routine when the parameter is 40 and a different
    routine when it is 20? Aren't parameters supposed to be variables?
    Can't one routine accept both a 20 or a 40 as a parameter?

    Even if the routines should be distinct, they need not be function
    templates. One function could be named Function20 and the other
    Function40. Non-type function templates provide no more abstraction
    than parameter-accepting functions, but can easily add significantly
    more bloat to an application.

    > However here I have another example :
    >
    > template <class T, int i> void lookup ( Buffer<T,i> b);
    >
    > void bar(Buffer<int, 128> b)
    > {
    > lookup(b,100); // No error, can infer T = int, i = 128
    > }
    >
    > So it seems that when 'i' gets inferred as a "side effect" of type
    > matching, the compiler accepts that. However, it doesnot try to infer
    > it explicitly if required.


    The compiler deduces that the type Buffer<int, 128> - so the 128 is
    deduced from the type of the parameter. You want the compiler to deduce
    40 from the value of the parameter - a completely different
    proposition.

    Greg
     
    Greg, Nov 10, 2005
    #2
    1. Advertising

  3. Neelesh

    Neelesh Guest

    Greg wrote:

    > The compiler deduces that the type Buffer<int, 128> - so the 128 is
    > deduced from the type of the parameter. You want the compiler to deduce
    > 40 from the value of the parameter - a completely different
    > proposition.


    Great. That last sentence just clears my confusion. Thanks a lot.
     
    Neelesh, Nov 10, 2005
    #3
  4. Neelesh

    John Carson Guest

    "Greg" <> wrote in message
    news:
    >
    > The compiler cannot deduce non-type parameters for function templates
    > since there is no requirement that the parameters be compile-time
    > constants.


    That is news to me. See if you can get this to compile

    template<int i>
    void foo()
    {
    int x=i;
    }
    int a = 5;

    int main()
    {
    foo<a>();
    }

    --
    John Carson
     
    John Carson, Nov 10, 2005
    #4
  5. Neelesh

    John Carson Guest

    "John Carson" <> wrote in message
    news:dkves3$lfd$
    > "Greg" <> wrote in message
    > news:
    >>
    >> The compiler cannot deduce non-type parameters for function templates
    >> since there is no requirement that the parameters be compile-time
    >> constants.

    >
    > That is news to me. See if you can get this to compile
    >
    > template<int i>
    > void foo()
    > {
    > int x=i;
    > }
    > int a = 5;
    >
    > int main()
    > {
    > foo<a>();
    > }
    >


    Or perhaps I misunderstood you and you meant that in

    template <class T, int i> void foo (T, int p = i) ;

    the ordinary (not template) p parameter of foo doesn't have to be a compile
    time constant.


    --
    John Carson
     
    John Carson, Nov 10, 2005
    #5
    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. Fei Liu
    Replies:
    0
    Views:
    442
    Fei Liu
    Oct 25, 2007
  2. Fei Liu
    Replies:
    4
    Views:
    820
    Victor Bazarov
    Oct 26, 2007
  3. KD
    Replies:
    4
    Views:
    348
    Road.Tang
    Jun 20, 2008
  4. er
    Replies:
    5
    Views:
    1,444
  5. Replies:
    2
    Views:
    370
Loading...

Share This Page