473,394 Members | 1,802 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Overloading with templates

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_iterat or,
std::vector<int,std::allocator<int>>::const_iterat or,
int)
object type is: Foo

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

I expected the compiler to select the templatized overload.

Cheers! --M

Apr 27 '06 #1
4 2275
mlimber wrote:
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_iterat or,
std::vector<int,std::allocator<int>>::const_iterat or,
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 27 '06 #2
Victor Bazarov wrote:
mlimber wrote:
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_iterat or,
std::vector<int,std::allocator<int>>::const_iterat or,
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".


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

Apr 27 '06 #3
mlimber wrote:
Victor Bazarov wrote:
mlimber wrote:
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_iterat or,
std::vector<int,std::allocator<int>>::const_iterat or,
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".


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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 27 '06 #4
Victor Bazarov wrote:
mlimber wrote:
Victor Bazarov wrote:
mlimber wrote:
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_iterat or,
std::vector<int,std::allocator<int>>::const_iterat or,
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".


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.


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

Cheers! --M

Apr 27 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Terje Slettebų | last post by:
To round off my trilogy of "why"'s about PHP... :) If this subject have been discussed before, I'd appreciate a pointer to it. I again haven't found it in a search of the PHP groups. The PHP...
4
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. ...
7
by: Hendrik Schober | last post by:
Hi, I have a problem, that boils down to the following code: #include <iostream> #include <typeinfo> class Test1 {}; class Test2 {}; class Test3 {};
16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
51
by: Jojo | last post by:
Is there any way to get to the left-hand side of an operator? Consider the following (this is not meant to be perfect code, just an example of the problem): class Matrix { public: int data;...
39
by: zeus | last post by:
I know function overloading is not supported in C. I have a few questions about this: 1. Why? is it from technical reasons? if so, which? 2. why wasn't it introduced to the ANSI? 3. Is there any...
6
by: TuxC0d3 | last post by:
Hi! I'm diving into the some more ++ specific aspects of c++ (and finally accepting that c++ is more than "a plus added to c" :), so that means using namespaces, templates, std::strings, lists,...
2
by: recover | last post by:
#include <stdio.h> template<class T> class TpHello { public: int GetHash(){return 0;} protected: private: T a;
11
by: jakester | last post by:
I am using Visual C++ 2007 to build the code below. I keep getting linkage error. Could someone please tell me what I am doing wrong? The code works until I start using namespace for my objects. ...
1
by: Zach | last post by:
Consider the following code: void Test(int i) { System.Console.WriteLine("int function"); } void Test(object o) { System.Console.WriteLine("object function");
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.