Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Explicit instantiation of STL vector demands explicit instantiation of all the templates it using internally. (http://www.velocityreviews.com/forums/t459431-explicit-instantiation-of-stl-vector-demands-explicit-instantiation-of-all-the-templates-it-using-internally.html)

krunalbauskar@gmail.com 12-25-2006 12:54 PM

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.


Salt_Peter 12-25-2006 03:51 PM

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

krunalbauskar@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<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<int> myVector;

>
> 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>
>




All times are GMT. The time now is 01:10 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.