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

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

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a

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.