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

K

krunalbauskar

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) << 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::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) << 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.
 
S

Salt_Peter

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<int> vn; // vn is an empty std::vector of integers
std::vector<int> v2(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 said:
for(int i= 0; i < 10; i++)
vec->push_back(i* 10);

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

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,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top