C++ - Is it possible to instantiate a `vector` without specifying the type? -


so basic, hard search in google me.

i doing c++ training course online , topic stl; in case vector.

is possible instantiate vector without specifying type?

#include <vector> #include <iostream>  using namespace std;  int main() {     vector v1(10, 0);     cout<<"size: "<<v1.size()<<endl;     for(unsigned = 0; < v1.size(); ++i)     {         cout<< v1[i]<<" ";     }     cout<<endl;     return 0; } 

i think wrong, see throughout course , confuses me.

when use vector<int> v1(10, 0) compiles, , how should think.

in course using netbeans, don't think there config or parameter or can make happen, there?

templates in general

ignoring details of std::vector moment, is possible define default type template parameter of class template. example:

template <class t = int> class foo {      t *bar; }; 

in such case, don't have specify type instantiate template. @ same time, do have include template parameter list. trick list can empty, instantiate template in of following ways:

foo<long> a; // instantiate on long. `int` default ignored foo<int>  b; // instantiate on int. still doesn't use default foo<>     c; // instantiates on int 

std::vector specifically

std::vector use default parameter type of allocator, not provide default type being stored, definition looks this:

template <class t, class allocator = std::allocator<t>> class vector // ... 

so, if don't specify otherwise, allocator type vector std::allocator instantiated on same type you're storing--but do have specify type you're storing, because no default provided type.

summary

it possible specify defaults parameters template, in case it's possible instantiate template without (explicitly) specifying type @ instantiation--but std::vector has 1 template parameter no default provided, instantiate vector, must specify type parameter.


Comments