Constructing a vector<T> without the type

Consider the following vector:

1
2
3
4
5
6
    vector<ios_base::iostate> vflgs
        {ios_base::goodbit, 
         ios_base::eofbit,
         ios_base::failbit,
         ios_base::badbit
        };


Theoretically, since the initializer_list specifies the type of the elements, it should be possible for vector<> to infer this type from there.

Therefore, we shouldn't have to specify the type again in the vector declaration:

 
    vector<ios_base::iostate> vflgs ...


However, a vector<auto> declaration is not possible.

Is there a factory function for vector<> which allows us to avoid having to respecify the vector's type in the declaration?

Thanks.
Added by C++17

'Class template argument deduction', 'Automatic deduction guides' and 'User-defined deduction guides'
http://en.cppreference.com/w/cpp/language/class_template_argument_deduction

Deduction guide for std::vector<> to allow deduction from a pair of iterators.
http://en.cppreference.com/w/cpp/container/vector/deduction_guides
Is there a factory function

No.

It should be possible for vector<> to infer this type from there.

It should, and it does, in C++17! This works using an automatically-generated deduction guide:
http://en.cppreference.com/w/cpp/language/class_template_argument_deduction

e.g.:
1
2
3
4
5
6
7
8
9
# include <ios>
# include <vector>

std::vector vflgs {
  std::ios_base::goodbit,
  std::ios_base::eofbit,
  std::ios_base::failbit,
  std::ios_base::badbit
};

http://coliru.stacked-crooked.com/a/ca2e977fc07e07c4

Last edited on
Thanks for this info.

And also good to know that coliru has C++17 support.

Is it feature-complete?

Thanks.
> Is it feature-complete?

No. The library is not feature-complete.
Topic archived. No new replies allowed.