About matching a template function

Discussion in 'C++' started by winterTTr, Jul 11, 2010.

  1. winterTTr

    winterTTr Guest

    Hi , I just want to write a template function to accomplish some kind
    of action.
    But i meet a problem that the compiler always say that "no matching
    function for call".
    There must be some problem about the function declaration or using,
    but i can't find it.
    Please help me to point out that the problem is, thanks.

    The code is like below( i remove the most of the detail part )

    template < typename _Iterator , typename _Compare >
    void bubbleSort(
    _Iterator __first ,
    _Iterator __last ,
    _Compare __compare = less< typename
    iterator_traits<_Iterator>::value_type >() )
    {
    typedef typename iterator_traits<_Iterator>::value_type
    _value_type;
    copy( __first , __last , ostream_iterator< _value_type > ( cout ,
    " " ) );
    };

    int main(int argc, char const* argv[])
    {
    vector<int> m;
    m.push_back( 1 );
    m.push_back( 2 );
    m.push_back( 3 );
    m.push_back( 4 );
    bubbleSort( m.begin() , m.end() ); //
    error !! Can't find the function to call
    bubbleSort( m.begin() , m.end() , less<int>() ); // this is OK.
    return 0;
    }


    Can you tell me why the error comes out when i want to use the default
    value?
    Or Is there a way to prevent this error as well as using the default
    value for the _Compare argument?
    winterTTr, Jul 11, 2010
    #1
    1. Advertising

  2. winterTTr <>, on 11/07/2010 04:13:55, wrote:

    > Hi , I just want to write a template function to accomplish some kind
    > of action.
    > But i meet a problem that the compiler always say that "no matching
    > function for call".
    > There must be some problem about the function declaration or using,
    > but i can't find it.
    > Please help me to point out that the problem is, thanks.
    >
    > The code is like below( i remove the most of the detail part )
    >
    > template< typename _Iterator , typename _Compare>
    > void bubbleSort(
    > _Iterator __first ,
    > _Iterator __last ,
    > _Compare __compare = less< typename
    > iterator_traits<_Iterator>::value_type>() )
    > {
    > typedef typename iterator_traits<_Iterator>::value_type
    > _value_type;
    > copy( __first , __last , ostream_iterator< _value_type> ( cout ,
    > " " ) );
    > };
    >
    > int main(int argc, char const* argv[])
    > {
    > vector<int> m;
    > m.push_back( 1 );
    > m.push_back( 2 );
    > m.push_back( 3 );
    > m.push_back( 4 );
    > bubbleSort( m.begin() , m.end() ); //
    > error !! Can't find the function to call
    > bubbleSort( m.begin() , m.end() , less<int>() ); // this is OK.
    > return 0;
    > }
    >
    >
    > Can you tell me why the error comes out when i want to use the default
    > value?
    > Or Is there a way to prevent this error as well as using the default
    > value for the _Compare argument?


    I don't really know the rationale or the details, but a similar function
    in the STL (std::sort) - at least on my MinGW implementation - isn't
    implemented as a template with a default argument as you're doing, but
    as (at least) two templates: one with the "compare" argument, and
    another without.

    Maybe the template system isn't able to deduce the "compare" argument
    even if the default given one bases itself on the "iterator" argument.

    By the way, you shouldn't be using compiler-reserved names as you're
    doing (leading underscore + uppercase, double leading underscore).

    Just my two cents, hope that helps.

    --
    FSC - http://userscripts.org/scripts/show/59948
    http://fscode.altervista.org - http://sardinias.com
    Francesco S. Carta, Jul 11, 2010
    #2
    1. Advertising

  3. winterTTr

    winterTTr Guest

    On Jul 11, 7:41 pm, "Francesco S. Carta" <> wrote:
    > winterTTr <>, on 11/07/2010 04:13:55, wrote:
    >
    >
    >
    > > Hi , I just want to write a template function to accomplish some kind
    > > of action.
    > > But i meet a problem that the compiler always say that "no matching
    > > function for call".
    > > There must be some problem about the function declaration or using,
    > > but i can't find it.
    > > Please help me to point out that the problem is, thanks.

    >
    > > The code is like below( i remove the most of the detail part )

    >
    > > template<  typename _Iterator , typename _Compare>
    > > void bubbleSort(
    > >          _Iterator __first ,
    > >          _Iterator __last ,
    > >          _Compare __compare = less<  typename
    > > iterator_traits<_Iterator>::value_type>() )
    > > {
    > >      typedef typename iterator_traits<_Iterator>::value_type
    > > _value_type;
    > >      copy( __first , __last , ostream_iterator<  _value_type>  ( cout ,
    > > " " ) );
    > > };

    >
    > > int main(int argc, char const* argv[])
    > > {
    > >      vector<int>  m;
    > >      m.push_back( 1 );
    > >      m.push_back( 2 );
    > >      m.push_back( 3 );
    > >      m.push_back( 4 );
    > >      bubbleSort( m.begin() , m.end() );                       //
    > > error !! Can't find the function to call
    > >      bubbleSort( m.begin() , m.end() , less<int>() );    // this is OK.
    > >      return 0;
    > > }

    >
    > > Can you tell me why the error comes out when i want to use the default
    > > value?
    > > Or Is there a way to prevent this error as well as using the default
    > > value for the _Compare argument?

    >
    > I don't really know the rationale or the details, but a similar function
    > in the STL (std::sort) - at least on  my MinGW implementation - isn't
    > implemented as a template with a default argument as you're doing, but
    > as (at least) two templates: one with the "compare" argument, and
    > another without.
    >
    > Maybe the template system isn't able to deduce the "compare" argument
    > even if the default given one bases itself on the "iterator" argument.


    As i know the template function should not able to have the default
    argument in template declaration but it can have default argument in
    function argument list. Maybe the template system isn't able to deduce
    the "compare" argument, as you said, and i am sure about this, either.
    I am just very curious now about the root problem for my function :)

    >
    > By the way, you shouldn't be using compiler-reserved names as you're
    > doing (leading underscore + uppercase, double leading underscore).

    OK, i will notice this problem and try to modify them in the real
    code.

    >
    > Just my two cents, hope that helps.

    Thanks for your answer and suggestion.

    >
    > --
    >   FSC -http://userscripts.org/scripts/show/59948
    >  http://fscode.altervista.org-http://sardinias.com
    winterTTr, Jul 11, 2010
    #3
  4. winterTTr

    winterTTr Guest

    On Jul 11, 7:41 pm, "Francesco S. Carta" <> wrote:
    > winterTTr <>, on 11/07/2010 04:13:55, wrote:
    >
    >
    >
    > > Hi , I just want to write a template function to accomplish some kind
    > > of action.
    > > But i meet a problem that the compiler always say that "no matching
    > > function for call".
    > > There must be some problem about the function declaration or using,
    > > but i can't find it.
    > > Please help me to point out that the problem is, thanks.

    >
    > > The code is like below( i remove the most of the detail part )

    >
    > > template<  typename _Iterator , typename _Compare>
    > > void bubbleSort(
    > >          _Iterator __first ,
    > >          _Iterator __last ,
    > >          _Compare __compare = less<  typename
    > > iterator_traits<_Iterator>::value_type>() )
    > > {
    > >      typedef typename iterator_traits<_Iterator>::value_type
    > > _value_type;
    > >      copy( __first , __last , ostream_iterator<  _value_type>  ( cout ,
    > > " " ) );
    > > };

    >
    > > int main(int argc, char const* argv[])
    > > {
    > >      vector<int>  m;
    > >      m.push_back( 1 );
    > >      m.push_back( 2 );
    > >      m.push_back( 3 );
    > >      m.push_back( 4 );
    > >      bubbleSort( m.begin() , m.end() );                       //
    > > error !! Can't find the function to call
    > >      bubbleSort( m.begin() , m.end() , less<int>() );    // this is OK.
    > >      return 0;
    > > }

    >
    > > Can you tell me why the error comes out when i want to use the default
    > > value?
    > > Or Is there a way to prevent this error as well as using the default
    > > value for the _Compare argument?

    >
    > I don't really know the rationale or the details, but a similar function
    > in the STL (std::sort) - at least on  my MinGW implementation - isn't
    > implemented as a template with a default argument as you're doing, but
    > as (at least) two templates: one with the "compare" argument, and
    > another without.
    >
    > Maybe the template system isn't able to deduce the "compare" argument
    > even if the default given one bases itself on the "iterator" argument.


    As i know the template function should not able to have the default
    argument in template declaration but it can have default argument in
    function argument list. Maybe the template system isn't able to deduce
    the "compare" argument, as you said, and i am sure about this, either.
    I am just very curious now about the root problem for my function :)

    >
    > By the way, you shouldn't be using compiler-reserved names as you're
    > doing (leading underscore + uppercase, double leading underscore).

    OK, i will notice this problem and try to modify them in the real
    code.

    >
    > Just my two cents, hope that helps.

    Thanks for your answer and suggestion.

    >
    > --
    >   FSC -http://userscripts.org/scripts/show/59948
    >  http://fscode.altervista.org-http://sardinias.com
    winterTTr, Jul 11, 2010
    #4
  5. winterTTr

    winterTTr Guest

    On Jul 11, 7:41 pm, "Francesco S. Carta" <> wrote:
    > winterTTr <>, on 11/07/2010 04:13:55, wrote:
    >
    >
    >
    > > Hi , I just want to write a template function to accomplish some kind
    > > of action.
    > > But i meet a problem that the compiler always say that "no matching
    > > function for call".
    > > There must be some problem about the function declaration or using,
    > > but i can't find it.
    > > Please help me to point out that the problem is, thanks.

    >
    > > The code is like below( i remove the most of the detail part )

    >
    > > template<  typename _Iterator , typename _Compare>
    > > void bubbleSort(
    > >          _Iterator __first ,
    > >          _Iterator __last ,
    > >          _Compare __compare = less<  typename
    > > iterator_traits<_Iterator>::value_type>() )
    > > {
    > >      typedef typename iterator_traits<_Iterator>::value_type
    > > _value_type;
    > >      copy( __first , __last , ostream_iterator<  _value_type>  ( cout ,
    > > " " ) );
    > > };

    >
    > > int main(int argc, char const* argv[])
    > > {
    > >      vector<int>  m;
    > >      m.push_back( 1 );
    > >      m.push_back( 2 );
    > >      m.push_back( 3 );
    > >      m.push_back( 4 );
    > >      bubbleSort( m.begin() , m.end() );                       //
    > > error !! Can't find the function to call
    > >      bubbleSort( m.begin() , m.end() , less<int>() );    // this is OK.
    > >      return 0;
    > > }

    >
    > > Can you tell me why the error comes out when i want to use the default
    > > value?
    > > Or Is there a way to prevent this error as well as using the default
    > > value for the _Compare argument?

    >
    > I don't really know the rationale or the details, but a similar function
    > in the STL (std::sort) - at least on  my MinGW implementation - isn't
    > implemented as a template with a default argument as you're doing, but
    > as (at least) two templates: one with the "compare" argument, and
    > another without.
    >
    > Maybe the template system isn't able to deduce the "compare" argument
    > even if the default given one bases itself on the "iterator" argument.


    As i know the template function should not able to have the default
    argument in template declaration but it can have default argument in
    function argument list. Maybe the template system isn't able to deduce
    the "compare" argument, as you said, and i am sure about this, either.
    I am just very curious now about the root problem for my function :)

    >
    > By the way, you shouldn't be using compiler-reserved names as you're
    > doing (leading underscore + uppercase, double leading underscore).

    OK, i will notice this problem and try to modify them in the real
    code.

    >
    > Just my two cents, hope that helps.

    Thanks for your answer and suggestion.

    >
    > --
    >   FSC -http://userscripts.org/scripts/show/59948
    >  http://fscode.altervista.org-http://sardinias.com
    winterTTr, Jul 11, 2010
    #5
  6. winterTTr

    winterTTr Guest

    On Jul 11, 8:01 pm, winterTTr <> wrote:
    > On Jul 11, 7:41 pm, "Francesco S. Carta" <> wrote:
    >
    >
    >
    > > winterTTr <>, on 11/07/2010 04:13:55, wrote:

    >
    > > > Hi , I just want to write a template function to accomplish some kind
    > > > of action.
    > > > But i meet a problem that the compiler always say that "no matching
    > > > function for call".
    > > > There must be some problem about the function declaration or using,
    > > > but i can't find it.
    > > > Please help me to point out that the problem is, thanks.

    >
    > > > The code is like below( i remove the most of the detail part )

    >
    > > > template<  typename _Iterator , typename _Compare>
    > > > void bubbleSort(
    > > >          _Iterator __first ,
    > > >          _Iterator __last ,
    > > >          _Compare __compare = less<  typename
    > > > iterator_traits<_Iterator>::value_type>() )
    > > > {
    > > >      typedef typename iterator_traits<_Iterator>::value_type
    > > > _value_type;
    > > >      copy( __first , __last , ostream_iterator<  _value_type>  ( cout ,
    > > > " " ) );
    > > > };

    >
    > > > int main(int argc, char const* argv[])
    > > > {
    > > >      vector<int>  m;
    > > >      m.push_back( 1 );
    > > >      m.push_back( 2 );
    > > >      m.push_back( 3 );
    > > >      m.push_back( 4 );
    > > >      bubbleSort( m.begin() , m.end() );                       //
    > > > error !! Can't find the function to call
    > > >      bubbleSort( m.begin() , m.end() , less<int>() );    // this is OK.
    > > >      return 0;
    > > > }

    >
    > > > Can you tell me why the error comes out when i want to use the default
    > > > value?
    > > > Or Is there a way to prevent this error as well as using the default
    > > > value for the _Compare argument?

    >
    > > I don't really know the rationale or the details, but a similar function
    > > in the STL (std::sort) - at least on  my MinGW implementation - isn't
    > > implemented as a template with a default argument as you're doing, but
    > > as (at least) two templates: one with the "compare" argument, and
    > > another without.

    >
    > > Maybe the template system isn't able to deduce the "compare" argument
    > > even if the default given one bases itself on the "iterator" argument.

    >
    > As i know the template function should not able to have the default
    > argument in template declaration but it can have default argument in
    > function argument list. Maybe the template system isn't able to deduce
    > the "compare" argument, as you said, and i am sure about this, either.
    > I am just very curious now about the root problem for my function :)
    >
    >
    >
    > > By the way, you shouldn't be using compiler-reserved names as you're
    > > doing (leading underscore + uppercase, double leading underscore).

    >
    > OK, i will notice this problem and try to modify them in the real
    > code.
    >
    >
    >
    > > Just my two cents, hope that helps.

    >
    > Thanks for your answer and suggestion.
    >
    >
    >
    > > --
    > >   FSC -http://userscripts.org/scripts/show/59948
    > >  http://fscode.altervista.org-http://sardinias.com

    >
    >


    OMG!! my network is not good enough so i re-send so several times.
    Please ignore the redundant replay.
    winterTTr, Jul 11, 2010
    #6
  7. On Jul 11, 1:13 pm, winterTTr <> wrote:
    > Hi , I just want to write a template function to accomplish some kind
    > of action.
    > But i meet a problem that the compiler always say that "no matching
    > function for call".
    > There must be some problem about the function declaration or using,
    > but i can't find it.
    > Please help me to point out that the problem is, thanks.
    >

    <snip - function template with default argument>

    > Can you tell me why the error comes out when i want to use the default
    > value?


    The problem is that the default argument does not participate in the
    deduction of the template arguments.
    This means that the compiler can't determine what type _Compare should
    stand for and consequently can't use your template.

    > Or Is there a way to prevent this error as well as using the default
    > value for the _Compare argument?


    Write a second, forwarding template:

    template < typename _Iterator >
    void bubbleSort(
    _Iterator __first ,
    _Iterator __last )
    {
    bubbleSort( __first, __last, less< typename
    iterator_traits<_Iterator>::value_type >());
    };

    Bart v Ingen Schenau
    Bart van Ingen Schenau, Jul 12, 2010
    #7
    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. Vaca Louca
    Replies:
    7
    Views:
    9,313
    Vaca Louca
    May 4, 2005
  2. Replies:
    3
    Views:
    695
  3. Peng Yu
    Replies:
    3
    Views:
    767
    Thomas J. Gritzan
    Oct 26, 2008
  4. Marc Bissonnette

    Pattern matching : not matching problem

    Marc Bissonnette, Jan 8, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    231
    Marc Bissonnette
    Jan 13, 2004
  5. Bobby Chamness
    Replies:
    2
    Views:
    225
    Xicheng Jia
    May 3, 2007
Loading...

Share This Page