Is This Legal Template Code? (Deduction Rules(?))

Discussion in 'C++' started by Chris Johnson, Aug 13, 2003.

  1. Greetings all:

    I come across an interesting question (to me anyway) and I do not
    know the answer. Code/Questions follow:

    #include <iostream>

    #if 0
    // uncommenting *should* make call ambigous because of
    // odering deduction of the function signature,
    // not the type of the parameter
    template<typename T>
    void t(T*, T const* = 0, ...) {}
    #endif

    // this is essentially the code presented in an earlier question
    // IRC that I stumbled on; gcc-3.2.2 compiles
    // doesn't denote that it's *correct* though
    template<typename T>
    void t(T const*, T*, T* = 0)
    { std::cout << "template func with one template parameter\n";}

    // signature match previous declaration of 't' so now it's on to
    // 'template' ordering rules of 'type parameter' at this point for
    // the compiler
    template<typename T, typename C>
    void t(C const*, C*, C* = 0)
    { std::cout << "template func with two template parameters\n"; }

    void example(int* p)
    {
    // calls t<T,C>
    t<int *>(p, p);
    // calls t<T>
    t<>(p, p);
    }

    int main(int argc, char** argv)
    {
    example(&argc);

    return 0;
    }

    The crux of my problem is the two seperate 'void t(...)' definitions.
    That is the part I am having trouble with.

    If this is legal can someone tersely explain why it works? I will do
    my own homework on coverting the terse to verbose. ;)

    If is not legal code then the obvious theory is I'm using a broken
    compiler! - This conclusion based on the fact it compiles and excutes
    instead of a compilation error for an ambigous declaration.

    For what it's worth my first reaction is this should still be an
    ambiguous call therefore not legal. Any input on this will be greatly
    appreciated in advance. Thanks!

    C Johnson

    P.S. Given the noise level ealier in this group - I hope my question
    is on topic and in the appropriate newsgroup! It's a joke - laugh ;)



    -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
    http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
    -----== Over 80,000 Newsgroups - 16 Different Servers! =-----
     
    Chris Johnson, Aug 13, 2003
    #1
    1. Advertising

  2. Chris Johnson wrote:
    > The crux of my problem is the two seperate 'void t(...)' definitions.
    > That is the part I am having trouble with.
    >
    > If this is legal can someone tersely explain why it works? I will do
    > my own homework on coverting the terse to verbose. ;)
    >
    > If is not legal code then the obvious theory is I'm using a broken
    > compiler! - This conclusion based on the fact it compiles and excutes
    > instead of a compilation error for an ambigous declaration.


    Comeau online complains that it isnt legal.You can try it
    out here yourself:

    http://www.comeaucomputing.com/tryitout/

    Christoph
     
    Christoph Rabel, Aug 13, 2003
    #2
    1. Advertising

  3. Chris Johnson

    tom_usenet Guest

    On 13 Aug 2003 06:02:49 -0700, (C Johnson)
    wrote:

    >So here it is as follows with the same questions as my original post:
    >
    >#include <iostream>
    >
    >template<typename T>
    >void t(T const*, T*, T* = 0)
    >{ std::cout << "template func with one template parameter\n";}
    >
    >template<typename T, typename C>
    >void t(C const*, C*, C* = 0)
    >{ std::cout << "template func with two template parameters\n"; }
    >
    >void example(int* p)
    >{
    > // calls t<T,C>
    > t<int *>(p, p);


    Here, TAD (template argument deduction) gives you only 1 possibility:

    t<int*, int>(int const*, int*, int*)

    There is no way to match the first template function (it could match
    t<int>(int const*, int*, int*), but not t<int*>(anything))

    > // calls t<T>
    > t<>(p, p);


    Again, TAD gives us only one choice:

    t<int>(int const*, int*, int*)

    The second template function cannot be chosen since T is non-deducable
    (it doesn't appear in any parameter).

    So it isn't ambiguous. A more interesting case is this call:

    t<int>(p, p);

    Now, TAD successfully deduces signatures for both templates:

    1: t<int>(int const*, int*, int*)
    2: t<int, int>(int const*, int*, int*)

    Partial ordering chooses the second since it is more specialized
    (mainly because it has a non-deducable extra template parameter).

    Tom
     
    tom_usenet, Aug 14, 2003
    #3
  4. Chris Johnson

    C Johnson Guest

    (tom_usenet) wrote in message news:<>...
    [...]
    > So it isn't ambiguous. A more interesting case is this call:
    >
    > t<int>(p, p);
    >
    > Now, TAD successfully deduces signatures for both templates:
    >
    > 1: t<int>(int const*, int*, int*)
    > 2: t<int, int>(int const*, int*, int*)
    >
    > Partial ordering chooses the second since it is more specialized
    > (mainly because it has a non-deducable extra template parameter).
    >
    > Tom


    Well I feel very silly for not being able to figure that out on my
    own. D'oh! Thanks for the insight.

    C Johnson
     
    C Johnson, Aug 14, 2003
    #4
    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. Gianni Mariani

    template deduction of array size

    Gianni Mariani, Dec 3, 2003, in forum: C++
    Replies:
    14
    Views:
    7,111
    Gianni Mariani
    Dec 4, 2003
  2. Replies:
    3
    Views:
    4,378
  3. Peng Yu

    Template argument deduction

    Peng Yu, Apr 16, 2005, in forum: C++
    Replies:
    1
    Views:
    527
    Mike Wahler
    Apr 16, 2005
  4. Fei Liu
    Replies:
    0
    Views:
    442
    Fei Liu
    Oct 25, 2007
  5. Fei Liu
    Replies:
    4
    Views:
    820
    Victor Bazarov
    Oct 26, 2007
Loading...

Share This Page