473,508 Members | 2,207 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
7 12798
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
5898
by: Jason Heyes | last post by:
To my understanding, std::vector does not use reference counting to avoid the overhead of copying and initialisation. Where can I get a reference counted implementation of std::vector? Thanks.
18
2845
by: Janina Kramer | last post by:
hi ng, i'm working on a multiplayer game for a variable number of players and on the client side, i'm using a std::vector<CPlayer> to store informatik about the players. CPlayer is a class that...
1
2196
by: Adam Teasdale Hartshorne | last post by:
I would be extremely grateful if somebody could tell me what as I getting wrong with this little bit of code to find the index of a particular element in a std::vector std::vector<int>...
8
5094
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
18
7164
by: ma740988 | last post by:
Trying to get more acclimated with the use of function objects. As part of my test, consider: # include <vector> # include <iostream> # include <algorithm> #include <stdexcept> #include...
32
69638
by: zl2k | last post by:
hi, c++ user Suppose I constructed a large array and put it in the std::vector in a function and now I want to return it back to where the function is called. I can do like this: ...
56
5699
by: Peter Olcott | last post by:
I am trying to refer to the same std::vector in a class by two different names, I tried a union, and I tried a reference, I can't seem to get the syntax right. Can anyone please help? Thanks
13
2238
by: imutate | last post by:
Hi, I am migrating some std::vectors to use a template instead, but I get an incomplete type error in a struct declaration. #include <vector> template < typename T > class Vec : public...
7
4677
by: imutate | last post by:
How do I implement << ala std::cout for vector template ? I already have the following: #include <vector> template < typename T > class Vec : public std::vector< T { public: Vec() { } Vec(...
0
7231
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7132
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...
1
7063
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
7504
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...
1
5059
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...
0
4720
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...
0
1568
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 ...
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
432
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...

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.