Overloading with templates

M

mlimber

Any ideas why this code:

#include <vector>

using namespace std;

struct Foo
{
void Bar( int, int, int );

template<typename T>
void Bar(
typename vector<T>::const_iterator,
typename vector<T>::const_iterator,
int );
};

void Baz()
{
Foo foo;
const vector<int> v( 10u );
foo.Bar( v.begin(), v.end(), 42 );
}

generates this compile-time error:

"ComeauTest.c", line 20: error: no instance of overloaded function
"Foo::Bar" matches the argument list

The argument types that you used are: (
std::vector<int,std::allocator<int>>::const_iterator,
std::vector<int,std::allocator<int>>::const_iterator,
int)
object type is: Foo

foo.Bar( v.begin(), v.end(), 42 );
^

I expected the compiler to select the templatized overload.

Cheers! --M
 
V

Victor Bazarov

mlimber said:
Any ideas why this code:

#include <vector>

using namespace std;

struct Foo
{
void Bar( int, int, int );

template<typename T>
void Bar(
typename vector<T>::const_iterator,
typename vector<T>::const_iterator,
int );
};

void Baz()
{
Foo foo;
const vector<int> v( 10u );
foo.Bar( v.begin(), v.end(), 42 );
}

generates this compile-time error:

"ComeauTest.c", line 20: error: no instance of overloaded function
"Foo::Bar" matches the argument list

The argument types that you used are: (
std::vector<int,std::allocator<int>>::const_iterator,
std::vector<int,std::allocator<int>>::const_iterator,
int)
object type is: Foo

foo.Bar( v.begin(), v.end(), 42 );
^

I expected the compiler to select the templatized overload.

The compiler cannot deduce that 'T' is 'int' from
vector<int>::const_iterator. It's not one of "deducible contexts".
And it has nothing to do with overloading.

V
 
M

mlimber

Victor said:
The compiler cannot deduce that 'T' is 'int' from
vector<int>::const_iterator. It's not one of "deducible contexts".

Can you elaborate and perhaps supply a work-around (other than explicit
qualification, preferably).
And it has nothing to do with overloading.

I'm trying to call one of the Foo::Bar() functions based on the
parameter types passed to the function. What should I call it?

Cheers! --M
 
V

Victor Bazarov

mlimber said:
Can you elaborate and perhaps supply a work-around (other than
explicit qualification, preferably).

Elaborate? Look in the Standard, 14.8.2.4/9, or in the news archives.

Workaround, eh? Try:

....
template<class I> void Bar(I, I, int);

In that case your 'I' should be 'std::vector<int>::const_iterator', and
you can then extract 'int' from it using 'value_type' or some such.

#include <vector>
using namespace std;
struct Foo
{
void Bar( int, int, int );
template<typename I> void Bar(I, I, int);
};

void Baz()
{
Foo foo;
const vector<int> v( 10u );
foo.Bar( v.begin(), v.end(), 42 );
foo.Bar( 1,2,3 );
}

The code above compiles fine, but it's up to you to see if it suits
your purposes.

V
 
M

mlimber

Victor said:
Elaborate? Look in the Standard, 14.8.2.4/9, or in the news archives.

Workaround, eh? Try:

...
template<class I> void Bar(I, I, int);

In that case your 'I' should be 'std::vector<int>::const_iterator', and
you can then extract 'int' from it using 'value_type' or some such.

#include <vector>
using namespace std;
struct Foo
{
void Bar( int, int, int );
template<typename I> void Bar(I, I, int);
};

void Baz()
{
Foo foo;
const vector<int> v( 10u );
foo.Bar( v.begin(), v.end(), 42 );
foo.Bar( 1,2,3 );
}

The code above compiles fine, but it's up to you to see if it suits
your purposes.

Ok, thanks. For reference, I used std::iterator_traits to get the
value_type.

Cheers! --M
 

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,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top