473,397 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 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 3045

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

Similar topics

7
by: zhou | last post by:
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...
5
by: Alexander Stippler | last post by:
Hello, I wrote an (templatized) operator==() and don't know why the compiler doesn't consider it. What prevents it from being chosen? class Data {}; template <typename D> class Vector {
4
by: C. Carbonera | last post by:
/* Hi, I have a problem with explicit instantiation of templates in Visual C++ 6.0. I have provided the source below. I have an example of a function template that produces incorrect output in...
2
by: David | last post by:
Assuming of course that the instantiation statement is 100% ok. As a matter of fact, that very instantiation passes successfuly on VC++ 7.1 (and Borland) but since I was suspicious that it was OK...
4
by: CoolPint | last post by:
I would be grateful if someone could point out if I am understanding correctly and suggest ways to improve. Sorry for the long message and I hope you will kindly bear with it. I have to make it...
3
by: Dilip | last post by:
I am stumbling my way through C++ templates and I keep running into way too many questions. Here is one: If a library writer exposes a function template like so: template<typename T> void...
6
by: Kaba | last post by:
Here is a short snippet of code that does not compile (tested with Vc8 and Comeau). This is because somehow Vector<0gets instantiated, for which the array size goes to 0. However, I don't quite get...
3
by: Steven T. Hatton | last post by:
Has anybody here used explicit instantiation of templates? Has it worked well? Are there any issues to be aware of? -- NOUN:1. Money or property bequeathed to another by will. 2. Something...
4
by: yuanhp_china | last post by:
I define a class in A.h: template <class Tclass A{ public: void get_elem( const T&) ; };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.