Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   About matching a template function (http://www.velocityreviews.com/forums/t727962-about-matching-a-template-function.html)

winterTTr 07-11-2010 11:13 AM

About matching a template function
 
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?

Francesco S. Carta 07-11-2010 11:41 AM

Re: About matching a template function
 
winterTTr <winterttr@gmail.com>, 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

winterTTr 07-11-2010 12:00 PM

Re: About matching a template function
 
On Jul 11, 7:41*pm, "Francesco S. Carta" <entul...@gmail.com> wrote:
> winterTTr <winter...@gmail.com>, 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 07-11-2010 12:01 PM

Re: About matching a template function
 
On Jul 11, 7:41*pm, "Francesco S. Carta" <entul...@gmail.com> wrote:
> winterTTr <winter...@gmail.com>, 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 07-11-2010 12:01 PM

Re: About matching a template function
 
On Jul 11, 7:41*pm, "Francesco S. Carta" <entul...@gmail.com> wrote:
> winterTTr <winter...@gmail.com>, 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 07-11-2010 12:04 PM

Re: About matching a template function
 
On Jul 11, 8:01*pm, winterTTr <winter...@gmail.com> wrote:
> On Jul 11, 7:41*pm, "Francesco S. Carta" <entul...@gmail.com> wrote:
>
>
>
> > winterTTr <winter...@gmail.com>, 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.

Bart van Ingen Schenau 07-12-2010 02:28 PM

Re: About matching a template function
 
On Jul 11, 1:13*pm, winterTTr <winter...@gmail.com> 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


All times are GMT. The time now is 12:24 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57