templates - why are the calls ambiguous?

Discussion in 'C++' started by Piotrek, Jan 15, 2014.

  1. Piotrek

    Piotrek Guest

    Below are two short programs:

    1)

    template<class T> void foo(){}

    int main()
    {
    &foo<int>;
    return 0;
    }

    2)

    template<int N> void foo() {int i = N;}

    int main()
    {
    &foo<4>;
    return 0;
    }

    None of them compiles when tested here:

    http://codepad.org/

    and compilation fails with "error: statement
    cannot resolve address of overloaded function"
    (which can be bypassed for program 2. with
    (void(*)())&foo<4> instead of &foo<4>)

    while both compile when tested here:

    http://www.compileonline.com/compile_cpp11_online.php

    Two things:

    1) I realize it's caused by a rule saying a template
    name is considered to always name a set of overloaded
    functions (hence the ambiguity), but is there any reason
    behind it other than "because the standard says so"?
    To me it looks like in each case there's only one
    possible instantiation with a given type/value, so where
    does the ambiguity come from? After all, what's
    the difference between my example and perfectly valid:

    template<class T> void foo(){}

    int main()
    {
    foo<int>();
    return 0;
    }

    ?

    2) Why do the examples compile fine with the second
    compiler? Is the discussed rule loosened or removed
    from C++11 standard?
    Piotrek, Jan 15, 2014
    #1
    1. Advertising

  2. Piotrek

    Öö Tiib Guest

    On Wednesday, 15 January 2014 20:33:24 UTC+2, Piotrek wrote:
    > 1) I realize it's caused by a rule saying a template
    > name is considered to always name a set of overloaded
    > functions (hence the ambiguity), but is there any reason
    > behind it other than "because the standard says so"?


    It did not. 13.4 was not clear enough about intentions
    in C++98 so implementations did as they wanted.

    > 2) Why do the examples compile fine with the second
    > compiler? Is the discussed rule loosened or removed
    > from C++11 standard?


    An issue was fixed and standard's intentions clarified.
    See that issue:
    http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#115

    Like you see the defect report was raised past millennia.
    Öö Tiib, Jan 15, 2014
    #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. Peter Meier
    Replies:
    5
    Views:
    550
    Andrew McDonagh
    Nov 14, 2005
  2. JKop
    Replies:
    3
    Views:
    450
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    854
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,741
    Smokey Grindel
    Dec 2, 2006
  5. recover
    Replies:
    2
    Views:
    783
    recover
    Jul 25, 2006
Loading...

Share This Page