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

Discussion in 'C++' started by krunalbauskar@gmail.com, Dec 25, 2006.

  1. Guest

    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::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) << 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, 2006
    #1
    1. Advertising

  2. Salt_Peter Guest

    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) << endl;
    > }
    > </snippet>
    >
    Salt_Peter, Dec 25, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    378
    Victor Bazarov
    Jul 14, 2005
  2. Replies:
    8
    Views:
    1,911
    Csaba
    Feb 18, 2006
  3. Steven T. Hatton
    Replies:
    3
    Views:
    369
    Craig Scott
    Dec 5, 2006
  4. Aaron Graham
    Replies:
    0
    Views:
    455
    Aaron Graham
    Apr 3, 2009
  5. Replies:
    1
    Views:
    53
Loading...

Share This Page