473,545 Members | 2,688 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

vector<T>::iter ator argument matching

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

#include<vector >
using namespace std;

template<typena me 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_cx x::__normal_ite rator<double*, std::vector<dou ble,
std::allocator< double >)'

If I specialize the test2 function with

void test2(std::vect or<double>::ite rator b)
{};

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

Regards,

/Patrik

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

#include<vector >
using namespace std;

template<typena me 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_cx x::__normal_ite rator<double*, std::vector<dou ble,
std::allocator< double >)'

If I specialize the test2 function with

void test2(std::vect or<double>::ite rator 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_A SSERT
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.co m wrote:
template<typena me 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_cx x::__normal_ite rator<double*, std::vector<dou ble,
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::__no rmal_iterator<d ouble*, std::vector<dou ble,
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.co m wrote:
Hi all,
I'm having a problem writing template functions that take
vector<T>::iter ator as arguments and I'm sure you guys can set me
straight. Like this:

#include<vector >
using namespace std;

template<typena me 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_cx x::__normal_ite rator<double*, std::vector<dou ble,
std::allocator< double >)'

If I specialize the test2 function with

void test2(std::vect or<double>::ite rator 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_A SSERT
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.co m wrote:
Hi all,
I'm having a problem writing template functions that take
vector<T>::iter ator as arguments and I'm sure you guys can set me
straight. Like this:
>
#include<vector >
using namespace std;
>
template<typena me 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_iterato r and std::set<const T>::const_itera tor
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
2257
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 != v.rend(); i++) { something = (*i); }
10
7050
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 clear(), in DOT.NET the capacity() is reduced to 0.
6
3769
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; strings.push_back("abc"); strings.push_back("xyz"); strings.push_back("lmnop");
16
9113
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; memset(pTmp, 0, nDataSize + 1); //memcpy(pTmp, &m_pData, nDataSize);
10
1937
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
2450
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 searchable by an element (protocol, remote ip etc). The file is of known max width (65) containing 8 columns of known max size. The columns are seperated by...
7
5751
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 help, thank you. I am a freshman about the STL. The following is the code. #include <set> #include <iostream> #include <vector> int main() {
10
6052
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 remove the elements with odd values from your list * and the even values from your vector.
8
3593
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 class simple_vector<that is a hand-rolled substitute for array<>. With the latter I have code that tracks all allocations and destructions, so I can...
3
5635
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 k = i+1;
0
7434
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7946
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7457
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6026
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5360
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1921
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
744
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.