By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,089 Members | 1,340 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,089 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
7 Replies


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

zhou wrote:
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


Jul 19 '05 #2

P: n/a
zhou wrote in news:bd************@ID-171337.news.dfncis.de:
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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #3

P: n/a
On Mon, 23 Jun 2003 16:44:08 -0400, "zhou" <yz***@medplus.com> wrote:
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 comeswhen 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
Jul 19 '05 #4

P: n/a
tom_usenet wrote in news:3e***************@news.easynet.co.uk:
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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #5

P: n/a
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
Jul 19 '05 #6

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

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

Any suggestions?

Yan
Jul 19 '05 #7

P: n/a
zhou wrote in news:bd************@ID-171337.news.dfncis.de:
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 news:gnu.gcc.help,
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 news:comp.sys.hp.hpux listed in my newsreader, which
might be of some help.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.