Q: how to force template instantiation on std::find() for vector

Z

zhou

Hi there,

We have a compiler specific issue which requires us to force template instantiation. This works fine. The problem comes
when I try using std:find() on vector. Since vector has no member function find() and I have to use std::find(). The
linker fails on unsatisified symbols in find().

I think this is because find() is a template function and I have to force instantiation. However, I donot know the
correct syntax of doing so.
Any suggestions?
Thanks.

Yan
 
C

Chandra Shekhar Kumar

std::find(v.begin(), v.end(), X) shud work fine....
cud u plz elaborate yr problem with using std::find() with vector??
 
R

Rob Williscroft

zhou wrote in
I tried adding this:

template vector<MyType>::iterator find(vector<MyType>::iterator,
vector<MyType>::iterator, const MyType&);

It didnot compile becuase "non-template used as template".

Then make it a template :)

template
vector<MyType>::iterator

find< vector<MyType>::iterator, const MyType & >

(
vector<MyType>::iterator,
vector<MyType>::iterator,
const MyType &
)
;

HTH

Rob.
 
T

tom_usenet

Hi there,

We have a compiler specific issue which requires us to force template instantiation. This works fine.

Are you sure you're solving this issue in the best way? Using explicit
instantiation of STL stuff isn't a great idea. Perhaps you can
elaborate?

The problem comes
when I try using std:find() on vector. Since vector has no member function find() and I have to use std::find(). The
linker fails on unsatisified symbols in find().

I think this is because find() is a template function and I have to force instantiation. However, I donot know the
correct syntax of doing so.
Any suggestions?

find has two template arguments, the iterator type, and the type of
the value to find. Assuming you've got a vector<int>, you need (in a
cpp file):

//deduces template arguments from signature
template
std::vector<int>::iterator
std::find<>(
std::vector<int>::iterator,
std::vector<int>::iterator,
int const&
);

template
std::vector<int>::const_iterator
std::find<>(
std::vector<int>::const_iterator,
std::vector<int>::const_iterator,
int const&
);


Alternatively, you could use find like this:

int* pos = find(&v.front(), &v.front() + v.size(), 10);

and then you'd be safe to instantiate like this:

template
int const* std::find<>(
int const*,
int const*,
int const&
);

template
int* std::find<>(
int*,
int*,
int const&
);

As you can see, implicit instatiation is definitely the way to go.

Tom
 
R

Rob Williscroft

tom_usenet wrote in
And you can omit the arg list:

template
vector<MyType>::iterator
find<>
(
vector<MyType>::iterator,
vector<MyType>::iterator,
const MyType &
);

The compiler should deduce it from the function signature.

Cool, I just tried it gcc (3.2) Ok, MSVC 7.1 failed to find a match.

Rob.
 
Z

zhou

I am still trying to find someone to elaborate this problem for me :)

The problem comes with there are STL templates in a shared library, which is built on HP-UX with gcc. For some reason,
the HP linker does not know how to instantiate templates in shared library and therefore we have to force template
instantiation in every executable that links with the shared library. (We do that by having all exeuctables include a
header that does template instantiation).

Yan
 
Z

zhou

Got a compiler error (gcc on HP-UX):

Template-id find< MyType*, const MyType& > in declaration of primary template ....

Any suggestions?

Yan
 
R

Rob Williscroft

zhou wrote in
Got a compiler error (gcc on HP-UX):

Template-id find< MyType*, const MyType& > in declaration of primary
template ....

Don't have access to HP-UX, you could try asking in though you'll need to give the gcc version. You should also post the
complete error message and a cut & paste of the explicit template
instantiation.

I tested with gcc 3.2 (MinGw) and it handled both the explcit syntax
find<Typelist ...>(arglist ...) that I gave and the briefer
find<>(arglist ..) syntax that Tom gave.

I also found listed in my newsreader, which
might be of some help.

Rob.
 

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

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top