About matching a template function

W

winterTTr

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?
 
F

Francesco S. Carta

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.
 
W

winterTTr

on 11/07/2010 04:13:55 said:
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.
 
W

winterTTr

on 11/07/2010 04:13:55 said:
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.
 
W

winterTTr

on 11/07/2010 04:13:55 said:
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.
 
W

winterTTr

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.



OMG!! my network is not good enough so i re-send so several times.
Please ignore the redundant replay.
 
B

Bart van Ingen Schenau

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.
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top