Function template type inference doesn't work with inheritance?

Discussion in 'C++' started by MikhailGorbachev@gmail.com, May 13, 2007.

  1. Guest

    I'm having a problem getting derived types to be inferred as valid
    parameters by a function template.

    template <typename T>
    struct Base {}

    template<typename T>
    struct Derived : public Base<T> {}

    template <typename T>
    void fn(Base<T>* b) { }

    When I call fn with a Derived<int>* gcc gives me the error "no
    matching function for call to..."
    Is there any way to get the function template to accept derived
    objects?

    Thanks,
    Alex Rubinsteyn
     
    , May 13, 2007
    #1
    1. Advertising

  2. On May 13, 4:50 pm, wrote:
    > I'm having a problem getting derived types to be inferred as valid
    > parameters by a function template.
    >
    > template <typename T>
    > struct Base {}
    >
    > template<typename T>
    > struct Derived : public Base<T> {}
    >
    > template <typename T>
    > void fn(Base<T>* b) { }


    // strictly not want you want but might do
    template <typename X>
    void fn(X* x)
    {
    Base<T>* b( x );
    ....
    }

    Or add this method - at least you know that it's only the derived ones
    that you're getting

    template <typename T>
    void fn(Derived<T>* x)
    {
    Base<T> * b = x;
    fn( b );
    }


    Or - hack but does the job - breaks some non-compliant compilers

    // strictly not want you want but might do
    template <typename X>
    void fn(X* x, Convertible<X *, Base<T> *>::type = 0 )
    {
    Base<T>* b( x );
    fn(b);
    }

    where Convertible is a special template class to determine if the type
    of X is one of the types you allow be used - it's probably overkill
    for this case.

    >
    > When I call fn with a Derived<int>* gcc gives me the error "no
    > matching function for call to..."
    > Is there any way to get the function template to accept derived
    > objects?
    >
    > Thanks,
    > Alex Rubinsteyn
     
    Gianni Mariani, May 13, 2007
    #2
    1. Advertising

  3. Zeppe Guest

    wrote:
    > I'm having a problem getting derived types to be inferred as valid
    > parameters by a function template.
    >
    > template <typename T>
    > struct Base {}
    >
    > template<typename T>
    > struct Derived : public Base<T> {}
    >
    > template <typename T>
    > void fn(Base<T>* b) { }
    >
    > When I call fn with a Derived<int>* gcc gives me the error "no
    > matching function for call to..."
    > Is there any way to get the function template to accept derived
    > objects?


    FAQ 5.8 http://www.parashift.com/c -faq-lite/how-to-post.html#faq-5.8
    If you don't post compilable code, usually we can't help.
    (btw, it seems that the section 5 of the faq is by far the most quoted,
    isn't it? :) )

    The answer is that it should work. The following compile correctly in
    both vc8 and gcc 3.4 (the ones that I have now).


    template <class T>
    class Base
    { };

    template <class T>
    class Derived
    : public Base<T>
    { };

    template <class T>
    void foo(const Base<T>*)
    { }

    int main()
    {
    Derived<int> d;
    foo(&d);

    return 0;
    }



    Regards,

    Zeppe
     
    Zeppe, May 13, 2007
    #3
    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. Ville Vainio
    Replies:
    2
    Views:
    356
    Shawn Wheatley
    Jul 16, 2004
  2. Dave Benjamin

    Type inference, lessons learned from Vyper

    Dave Benjamin, Feb 3, 2005, in forum: Python
    Replies:
    0
    Views:
    314
    Dave Benjamin
    Feb 3, 2005
  3. Brett C.
    Replies:
    3
    Views:
    293
    Mark Lawrence
    Feb 15, 2014
  4. Niklas Matthies
    Replies:
    0
    Views:
    839
    Niklas Matthies
    Oct 24, 2006
  5. Andrew Tomazos

    Template Type Inference of Reference

    Andrew Tomazos, May 7, 2009, in forum: C++
    Replies:
    7
    Views:
    465
    Victor Bazarov
    May 8, 2009
Loading...

Share This Page