ambiguous or not?

Discussion in 'C++' started by catcher, Jun 24, 2003.

  1. catcher

    catcher Guest

    template<typename T>
    void fun(T x)
    {};

    void fun(int x)
    {};

    int main()
    {
    fun<int>(3);
    }

    This code is ambiguous or not?
    What the standard says about it?
    catcher, Jun 24, 2003
    #1
    1. Advertising

  2. "catcher" <> wrote...
    > template<typename T>
    > void fun(T x)
    > {};


    Please lose the semicolon

    >
    > void fun(int x)
    > {};


    Please lose the semicolon

    >
    > int main()
    > {
    > fun<int>(3);
    > }
    >
    > This code is ambiguous or not?
    > What the standard says about it?


    No, it is not ambiguous. The template function instantiated
    for 'T == int' should be used. And even if you drop the <int>
    thing after the name, it's still unambiguous -- the regular
    function should be used. See Clause 14, subs 14.2 and 14.8.3
    along with 13.3.3...

    Victor
    Victor Bazarov, Jun 24, 2003
    #2
    1. Advertising

  3. catcher

    catcher Guest

    I checked the standard, It says that the common function has higher
    priority than template function, when ambiguity happens. so fun(3)
    will call a common function.
    But I cant understand why fun<int>(3) should call a template function.
    I cant find the item about it in the standard. In fact, I compiled
    these codes using Intel C++, and the result is insteresting:
    fun<int>(3) call a common function, not a template function.
    catcher, Jun 26, 2003
    #3
  4. "catcher" <> wrote...
    > I checked the standard, It says that the common function has higher
    > priority than template function, when ambiguity happens. so fun(3)
    > will call a common function.
    > But I cant understand why fun<int>(3) should call a template function.
    > I cant find the item about it in the standard. In fact, I compiled
    > these codes using Intel C++, and the result is insteresting:
    > fun<int>(3) call a common function, not a template function.


    It is quite possible that I'm not reading it correctly. Asking
    in comp.std.c++ is advised. I understand it that if the template
    argument list is specified, then non-template functions are not
    added to the set of candidate functions. However, 14.8.3/1 has
    the following sentence: "The complete set of candidate functions
    includes all the function templates instantiated in this way and
    all of the non-template overloaded functions of the same name"
    which may be interpreted that only the NAME ('fun' in your case)
    is going to be considered for identification purposes.

    However, after further search I found 14.3/7, which says, "When
    the template in a template-id is an overloaded function template,
    both non-template functions in the overload set and function
    templates in the overload set for which the template-arguments do
    not match the template-parameters are ignored. If none of the
    function templates have matching template-parameters, the program
    is ill-formed." [To help understand, "template-id" is formed by
    an identifier followed by an optionally empty set of angle brackets,
    as in "fun<int>"]

    That seems pretty straight-forward to me. 'fun<int>' should force
    the compiler to ignore 'fun' or 'fun<double>' or whatever else is
    out there.

    Victor
    Victor Bazarov, Jun 26, 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. Tuukka Toivonen

    Ambiguous type?

    Tuukka Toivonen, Apr 30, 2004, in forum: VHDL
    Replies:
    3
    Views:
    714
    Stefan Frank
    May 3, 2004
  2. Replies:
    2
    Views:
    948
  3. Paul Johnson
    Replies:
    2
    Views:
    426
    Paul Johnson
    May 23, 2005
  4. sks_cpp
    Replies:
    8
    Views:
    335
    tom_usenet
    Aug 26, 2003
  5. REH
    Replies:
    16
    Views:
    658
    Marcelo Pinto
    Mar 31, 2005
Loading...

Share This Page