471,320 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,320 software developers and data experts.

Explicit instantiation of STL vector demands explicit instantiation of all the templates it using internally.

Hi,

Explicit instantiation of STL vector demands explicit instantiation of
all the templates it using internally.

For example -
<snippet>
#include <iostream>
#include <vector>

using namespace std;

template class vector<int>;
int main()
{
vector<int>* vec = new vector<int>();

for(int i= 0; i < 10; i++)
vec->push_back(i* 10);

for(int i= 0; i < 10; i++)
cout << " Number " << i << " :" << (*vec)[i] << endl;
}
</snippet>

If compiled with
gcc -fno-implicit-templates vector.cc -lstdc++
throws an error as vector internally uses some more function templates.
Corrected version:
<snippet>
#include <iostream>
#include <vector>

using namespace std;

template class vector<int>;
template class vector<int>;
template void std::fill<__gnu_cxx::__normal_iterator<int*,
std::vector<int, std::
:allocator<int >, int>(__gnu_cxx::__normal_iterator<int*,
std::vector<int, stt
d::allocator<int >, __gnu_cxx::__normal_iterator<int*,
std::vector<int, std:::
allocator<int >, int const&);
template __gnu_cxx::__normal_iterator<int*, std::vector<int,
std::allocator<int>>
std::fill_n<__gnu_cxx::__normal_iterator<int*, std::vector<int,
std::allocaa
tor<int >, unsigned int, int>(__gnu_cxx::__normal_iterator<int*,
std::vector<<
int, std::allocator<int >, unsigned int, int const&);
template int* std::fill_n<int*, unsigned int, int>(int*, unsigned int,
int constt
&);
int main()
{
vector<int>* vec = new vector<int>();

for(int i= 0; i < 10; i++)
vec->push_back(i* 10);

for(int i= 0; i < 10; i++)
cout << " Number " << i << " :" << (*vec)[i] << endl;
}
</snippet>

But I need to manually look at error and copy paste all the required
templates.

Is there an automated way/option using which I can avoid this.

Dec 25 '06 #1
1 2822

kr***********@gmail.com wrote:
Hi,

Explicit instantiation of STL vector demands explicit instantiation of
all the templates it using internally.

For example -
<snippet>
#include <iostream>
#include <vector>

using namespace std;

template class vector<int>;
The above is not a template or a class definition.
If what you wanted to do was design a class based on a std::vector
*member* and template the class with a template parameter:

template< typename T >
class Vector // Vector, not vector
{
std::vector< T vt; // private
public:
Vector() : vt() { }
Vector(const Vector& copy) { vt = copy.vt; }
void push_back(const T& r_t) { vt.push_back(r_t); }
// and so on
};

See below if what you want is simply an instantiation of a std::vector.
The STL containers, like std::vector, are declared in the namespace
std.
int main()
{
vector<int>* vec = new vector<int>();
Do not allocate with new. Forget using pointers too.
Use the stack instead of the heap unless using the stack is not an
option.
Even when the heap should be used, a smart pointer is the way to go.
Here is a couple of containers using the STL std::vector:

std::vector<intvn; // vn is an empty std::vector of integers
std::vector<intv2(100, 9); // v2 is a std::vector of 100 elements
all initialized

And another container which declares an instance of the above Vector
class:

Vector<intmyVector;
>
for(int i= 0; i < 10; i++)
vec->push_back(i* 10);

for(int i= 0; i < 10; i++)
cout << " Number " << i << " :" << (*vec)[i] << endl;
}
</snippet>
Dec 25 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Alexander Stippler | last post: by
6 posts views Thread by Kaba | last post: by
3 posts views Thread by Steven T. Hatton | last post: by
reply views Thread by rosydwin | last post: by

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.