通用矢量的填充构造函数

Universal vector's fill constructor

本文关键字:填充 构造函数      更新时间:2023-10-16

在C 17中是否有可能调用此构造函数

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

使用vector<int>的统一初始化?std::vector<int> data{10, 20}似乎创建了一个大小的向量。

有可能吗?当然。

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

只要value_typesize_type不同。

你应该这样做吗?否。

确定:

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

但是括号内没有任何天生的错误。