C ++ - Is it possible to instantiate a `vector` without specifying a type?
So basic but hard to google for me.
I am doing an online C ++ tutorial and the topic is STL; in this case vector
.
Is it possible to instantiate vector
without specifying a type?
#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector v1(10, 0);
cout<<"Size: "<<v1.size()<<endl;
for(unsigned i = 0; i < v1.size(); ++i)
{
cout<< v1[i]<<" ";
}
cout<<endl;
return 0;
}
I think this is wrong, but I see it confusing me throughout the course.
It vector<int> v1(10, 0)
compiles when used , which is exactly what I think.
We are using NetBeans in the course, but I don't think there is a config or parameter or something that can happen, is there?
source to share
Templates in general
Ignoring the data std::vector
for now, you can define a default type for the class template template parameter. For example:
template <class T = int>
class foo {
T *bar;
};
In this case, you do not need to specify the type to instantiate this template. At the same time, you need to include a list of template parameters. The trick is that the list can be empty, so you can instantiate this template in any of the following ways:
foo<long> a; // instantiate over long. The `int` default is just ignored
foo<int> b; // instantiate over int. Still doesn't use default
foo<> c; // also instantiates over int
std::vector
in particular
std::vector
uses a default parameter for the allocator type, but does not provide a default value for the stored type, so the definition looks something like this:
template <class T, class allocator = std::allocator<T>>
class vector
// ...
So, unless you indicate otherwise, the type of the allocator for the vector will be std::allocator
created by the same type that you store, but you always need to specify the type that you store because there is no default for that type.
Summary
It is definitely possible to specify default values ββfor all template parameters, in which case it is possible to instantiate the template without (explicitly) specifying the type when instantiating, but std::vector
has one template parameter for which there is no default, so to instantiate vector
you need to specify the type for this parameter.
source to share
You must specify the type because it is not inferred from the constructor parameters. However, no one forbids you to do this.
std::vector<int> make_vector(std::vector<int>::size_type n, int val)
{
return std::vector<int>(n, val);
}
// ...
auto v2 = make_vector(10, 0);
How impossible it is, see this question and related questions.
source to share
Perhaps (with a different setup):
#include <vector>
#include <iostream>
using std::cout;
using std::endl;
using vector = std::vector<int>;
int main()
{
vector v1(10, 0);
cout<<"Size: "<<v1.size()<<endl;
for(unsigned i = 0; i < v1.size(); ++i)
{
cout<< v1[i]<<" ";
}
cout<<endl;
return 0;
}
Note, use "using" carefully.
source to share
C ++ 17 supports creating typeless vectors. Please see this article, https://en.cppreference.com/w/cpp/language/class_template_argument_deduction
for more information.
So, for example, writing this would work:
vector v {1, 2, 3}; // instead of vector<int>
if you compile with this flag the "-std = c ++ 17" flag.
source to share