user2146414
user2146414

Reputation: 1038

Universal initialization - vector's fill constructor

Is it possible in c++17 to call this constructor

 vector( size_type count, const T& value, const Allocator& alloc = Allocator());

using uniform initialization for vector<int>? It seems that std::vector<int> data{10, 20} creates a vector of size two.

Upvotes: 0

Views: 280

Answers (2)

Barry
Barry

Reputation: 303487

Sure:

std::vector<int> vi{10, 4, std::allocator<int>()};

But there's nothing inherently wrong with parentheses.

Upvotes: 4

T.C.
T.C.

Reputation: 137425

Is it possible? Sure.

struct size_type {
    template<class T, std::enable_if_t<std::is_same_v<T, std::vector<int>::size_type>>* = nullptr>
    operator T() const {
        return val;
    }
    std::vector<int>::size_type val;
};

std::vector<int> vi {size_type{10}, 4}; // vector of 10 ints with value 4

This works as long as value_type is not the same type as size_type.

Should you do it? No.

Upvotes: 4

Related Questions