473,395 Members | 1,742 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,395 software developers and data experts.

vector<T>::iterator argument matching

Hi all,
I'm having a problem writing template functions that take
vector<T>::iterator as arguments and I'm sure you guys can set me
straight. Like this:

#include<vector>
using namespace std;

template<typename T>
void test2(typename std::vector<T>::iterator b)
{};

void test() {
vector<doublev;
test2(v.begin());
}

If compiled with g++, I get

junk.cc:11: error: no matching function for call to
'test2(__gnu_cxx::__normal_iterator<double*, std::vector<double,
std::allocator<double >)'

If I specialize the test2 function with

void test2(std::vector<double>::iterator b)
{};

it compiles fine. I don't understand why it's not picking up the
templated version.

Regards,

/Patrik

Sep 14 '06 #1
4 2565
lu****@gmail.com wrote:
Hi all,
I'm having a problem writing template functions that take
vector<T>::iterator as arguments and I'm sure you guys can set me
straight. Like this:

#include<vector>
using namespace std;

template<typename T>
void test2(typename std::vector<T>::iterator b)
{};

void test() {
vector<doublev;
test2(v.begin());
}

If compiled with g++, I get

junk.cc:11: error: no matching function for call to
'test2(__gnu_cxx::__normal_iterator<double*, std::vector<double,
std::allocator<double >)'

If I specialize the test2 function with

void test2(std::vector<double>::iterator b)
{};

it compiles fine. I don't understand why it's not picking up the
templated version.

Regards,

/Patrik
It's a dependent name in the context of your function prototype, and so
it doesn't participate in name lookup. Try this:

template<class FwdIter>
void test2( FwdIter ) {}

If you want you can put some sort of static check (BOOST_STATIC_ASSERT
or Loki's STATIC_CHECK) in the body of the function to make sure you'll
get a compile-time error if the type is not a vector's iterator.

Cheers! --M

Sep 14 '06 #2
lu****@gmail.com wrote:
template<typename T>
void test2(typename std::vector<T>::iterator b)
{};

void test() {
vector<doublev;
test2(v.begin());
}

If compiled with g++, I get

junk.cc:11: error: no matching function for call to
'test2(__gnu_cxx::__normal_iterator<double*, std::vector<double,
std::allocator<double >)'
Well, it works if you write:
test2<double>( v.begin() );

As to why your code doesn't work, I don't know exactly. But in
case nobody else knows either, here's my guess: the compiler
is trying to find a function with a parameter that matches:

__gnu_cxx::__normal_iterator<double*, std::vector<double,
std::allocator<double >

If it is to match this with:
std::vector<T>::iterator

then it would have to go through every possible option for
T and the see whether std::vector<T>::iterator matched the
type that it's searching for.

This kind of search is something that compilers normally
aren't expected to do (perhaps because it's slow, or
because it leads to fragile code).

The normal way to make a function that takes an iterator
is just to make the entire iterator type the template
parameter, and not worry about what sort of container
it is. But you might be able to do something similar to
what you are doing now, by using the advanced Boost
iterator concepts, which I'm not familiar with myself.

Sep 14 '06 #3

mlimber wrote:
lu****@gmail.com wrote:
Hi all,
I'm having a problem writing template functions that take
vector<T>::iterator as arguments and I'm sure you guys can set me
straight. Like this:

#include<vector>
using namespace std;

template<typename T>
void test2(typename std::vector<T>::iterator b)
{};

void test() {
vector<doublev;
test2(v.begin());
}

If compiled with g++, I get

junk.cc:11: error: no matching function for call to
'test2(__gnu_cxx::__normal_iterator<double*, std::vector<double,
std::allocator<double >)'

If I specialize the test2 function with

void test2(std::vector<double>::iterator b)
{};

it compiles fine. I don't understand why it's not picking up the
templated version.

Regards,

/Patrik

It's a dependent name in the context of your function prototype, and so
it doesn't participate in name lookup. Try this:
Its a dependent name alright, but I am not sure if that is the reason
the compiler is failing.

Isnt std::vector<T>::iterator above actually form a non-deduced context
? I think that's the reason the compiler cant compile it.
>
template<class FwdIter>
void test2( FwdIter ) {}

If you want you can put some sort of static check (BOOST_STATIC_ASSERT
or Loki's STATIC_CHECK) in the body of the function to make sure you'll
get a compile-time error if the type is not a vector's iterator.

Cheers! --M
Sep 14 '06 #4
am******@gmail.com wrote:
lu****@gmail.com wrote:
Hi all,
I'm having a problem writing template functions that take
vector<T>::iterator as arguments and I'm sure you guys can set me
straight. Like this:
>
#include<vector>
using namespace std;
>
template<typename T>
void test2(typename std::vector<T>::iterator b)
{};
>
void test() {
vector<doublev;
test2(v.begin());
}
Isnt std::vector<T>::iterator above actually form a non-deduced context
? I think that's the reason the compiler cant compile it.
It is.

The reason it's non-deduced is because in general Foo<T>::Bar and
Foo<U>::Bar can be the same type, say int. That means that you cannot
choose between T and U, if all you know is that the actual argument is
1.

Now, std::vector<T>::iterator is defined in the library in a way that
std::vector<T>::iterator != std::vector<U>::iterator if T != U, but the
core language doesn't have exceptions for this.
(std::vector<T>::const_iterator and std::set<const T>::const_iterator
might
actually be the same, which shows how tricky it is)

Regards,
Michiel Salters

Sep 14 '06 #5

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

Similar topics

0
by: Marc Schellens | last post by:
my dinkumware docu says, vector<...>::rbegin() returns an iterator which points just BEYOND the end of the controlled sequence. Is that true? so I cannot say: for( riter i=v.rbegin(); i !=...
10
by: Stefan Höhne | last post by:
Hi, as I recon, std::vector::clear()'s semantics changed from MS VC++ 6.0 to MS' DOT.NET - compiler. In the 6.0 version the capacity() of the vector did not change with the call to...
6
by: Joe | last post by:
I have a: vector<string> which contains a few dozen elements. I want to find the index of the element containing a certain string. for example: vector<string> strings;...
16
by: Vince | last post by:
Hi, I have replaced my BYTE* by a vector<BYTE> and before I used to do : void CCardRecord::GetRecData(int nOffset, int nDataSize, CString& csValue) { BYTE *pTmp = NULL; pTmp = new BYTE;...
10
by: nerdrakesh | last post by:
Hi - Is there a method in STL vector to get the elements as an array instead of as a vector. Something like vector<intvt; vt.push_back(1); vt.push_back(2);
5
by: Etrex | last post by:
Hello, This is my first attempt at a c++ program, and it is a long post, please bear with me. I'm trying to read in a text file containing a firewall log, make the information...
7
by: Renzr | last post by:
I have a problem about the std::set<>iterator. After finding a term in the std::set<>, i want to know the distance from the current term to the begin(). But i have got a error. Please offer me...
10
by: arnuld | last post by:
WANTED: /* C++ Primer - 4/e * * Exercise: 9.26 * STATEMENT * Using the following definition of ia, copy ia into a vector and into a list. Use the single iterator form of erase to...
8
by: jacek.dziedzic | last post by:
Hi! I need to be able to track memory usage in a medium-sized application I'm developing. The only significant (memory-wise) non- local objects are of two types -- std::vector<and of a custom...
3
by: Rune Allnor | last post by:
Hi folks. I have a function that takes an element in a vector as argument. The naive interface goes as float computeSomething(const std::vector<float>& v, size_t i) { size_t j = i-1; size_t...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.