Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > About matching a template function

Reply
Thread Tools

About matching a template function

 
 
winterTTr
Guest
Posts: n/a
 
      07-11-2010
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?
 
Reply With Quote
 
 
 
 
Francesco S. Carta
Guest
Posts: n/a
 
      07-11-2010
winterTTr <(E-Mail Removed)>, 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
 
Reply With Quote
 
 
 
 
winterTTr
Guest
Posts: n/a
 
      07-11-2010
On Jul 11, 7:41*pm, "Francesco S. Carta" <(E-Mail Removed)> wrote:
> winterTTr <(E-Mail Removed)>, 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



 
Reply With Quote
 
winterTTr
Guest
Posts: n/a
 
      07-11-2010
On Jul 11, 7:41*pm, "Francesco S. Carta" <(E-Mail Removed)> wrote:
> winterTTr <(E-Mail Removed)>, 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



 
Reply With Quote
 
winterTTr
Guest
Posts: n/a
 
      07-11-2010
On Jul 11, 7:41*pm, "Francesco S. Carta" <(E-Mail Removed)> wrote:
> winterTTr <(E-Mail Removed)>, 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



 
Reply With Quote
 
winterTTr
Guest
Posts: n/a
 
      07-11-2010
On Jul 11, 8:01*pm, winterTTr <(E-Mail Removed)> wrote:
> On Jul 11, 7:41*pm, "Francesco S. Carta" <(E-Mail Removed)> wrote:
>
>
>
> > winterTTr <(E-Mail Removed)>, 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.
 
Reply With Quote
 
Bart van Ingen Schenau
Guest
Posts: n/a
 
      07-12-2010
On Jul 11, 1:13*pm, winterTTr <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use the template member function of a template in the memberfunction of another template class? Peng Yu C++ 3 10-26-2008 03:51 PM
Passing pointer to template function as argument to pointer to template function Vijai Kalyan C++ 4 11-08-2005 07:53 PM
parse error in gcc but success in vc.net, call a non_template class's template member function from a template class's member function! ken C++ 2 06-28-2005 06:57 AM
Template function as argument to another template function Jim West C++ 3 10-07-2004 07:21 PM
function non-template versus fully specialized function template Robert Allan Schwartz C++ 1 08-09-2004 03:41 PM



Advertisments