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

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

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

    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<typena me 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<typena me 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<typena me 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! =-----
  • Christoph Rabel

    #2
    Re: Is This Legal Template Code? (Deduction Rules(?))

    Chris Johnson wrote:[color=blue]
    > 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.[/color]

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



    Christoph

    Comment

    • tom_usenet

      #3
      Re: Is This Legal Template Code? (Deduction Rules(?))

      On 13 Aug 2003 06:02:49 -0700, devcjohnson@exc ite.com (C Johnson)
      wrote:
      [color=blue]
      >So here it is as follows with the same questions as my original post:
      >
      >#include <iostream>
      >
      >template<typen ame T>
      >void t(T const*, T*, T* = 0)
      >{ std::cout << "template func with one template parameter\n";}
      >
      >template<typen ame 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);[/color]

      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*>(anythin g))
      [color=blue]
      > // calls t<T>
      > t<>(p, p);[/color]

      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

      Comment

      • C Johnson

        #4
        Re: Is This Legal Template Code? (Deduction Rules(?))

        tom_usenet@hotm ail.com (tom_usenet) wrote in message news:<3f3b5d86. 518598390@news. easynet.co.uk>. ..
        [...][color=blue]
        > 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[/color]

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

        C Johnson

        Comment

        Working...