Is it legal to assign a template function to a function pointer this way

Discussion in 'C++' started by cindypwl@lycos.com, Mar 19, 2006.

  1. Guest

    I was looking at some code in the MS NG, and I saw this code listed.
    template <class T>class List{};

    template<class T>
    bool MySortFunc(List<T>& list){return true;}

    template <class T>struct SORT{typedef bool (*FnPtr)(List<T>& list);};

    int main(){
    List<int> MyList;
    List<long> MyList2;
    SORT<int>::FnPtr Sort = &MySortFunc; //***No template type!!!!!!!
    Sort(MyList);

    I would think that the function pointer assignment would not compile
    with out using the templae type.
    SORT<int>::FnPtr Sort = &MySortFunc<int>;

    But I was able to compile this with out the type on both VC++ and
    Comeau.

    So is this code legal, or are the compilers wrong to compile it
    successfully?
     
    , Mar 19, 2006
    #1
    1. Advertising

  2. On 19 Mar 2006 05:00:16 -0800, wrote:

    >I was looking at some code in the MS NG, and I saw this code listed.
    >template <class T>class List{};
    >
    >template<class T>
    >bool MySortFunc(List<T>& list){return true;}
    >
    >template <class T>struct SORT{typedef bool (*FnPtr)(List<T>& list);};
    >
    >int main(){
    > List<int> MyList;
    > List<long> MyList2;
    > SORT<int>::FnPtr Sort = &MySortFunc; //***No template type!!!!!!!
    > Sort(MyList);
    >
    >I would think that the function pointer assignment would not compile
    >with out using the templae type.
    >SORT<int>::FnPtr Sort = &MySortFunc<int>;
    >
    >But I was able to compile this with out the type on both VC++ and
    >Comeau.
    >
    >So is this code legal, or are the compilers wrong to compile it
    >successfully?


    It is legal. The compiler instantiates MySortFunc<int> automatically
    because it is the only possible candidate. Otherwise, you might have
    to disambiguate it. The compiler is very clever in this regard (well,
    most modern compilers are, anyway).

    --
    Bob Hairgrove
     
    Bob Hairgrove, Mar 19, 2006
    #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. Ajax Chelsea
    Replies:
    1
    Views:
    444
    Ron Natalie
    Dec 2, 2003
  2. Vijai Kalyan
    Replies:
    4
    Views:
    745
    Vijai Kalyan
    Nov 8, 2005
  3. nutty
    Replies:
    4
    Views:
    401
    nutty
    Jun 11, 2006
  4. Ingo
    Replies:
    2
    Views:
    584
  5. avasilev
    Replies:
    4
    Views:
    595
    avasilev
    Dec 22, 2011
Loading...

Share This Page