通用矢量的填充构造函数
Universal vector's fill constructor
在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_type
与size_type
不同。
你应该这样做吗?否。
确定:
std::vector<int> vi{10, 4, std::allocator<int>()};
但是括号内没有任何天生的错误。
相关文章:
- 填充上编译器生成的复制构造函数之间的不一致
- 使用所述填充构造函数初始化向量中的向量
- C++:SFINAE 来区分填充和范围构造函数?
- 如何在C++11中区分填充构造函数和范围构造函数
- 用构造函数调用填充向量
- 组合 std::vector 默认值和填充构造函数
- 通用矢量的填充构造函数
- 为什么 C++11 从 std::vector 的填充构造函数的原型中删除了默认值?
- 通过指针填充自定义的类 - 与构造函数的麻烦
- 如何基于构造函数参数填充常量成员数组
- C++:在类构造函数中填充向量后,向量成员被销毁
- 结构错误的构造函数(调用类的构造函数 denconstructor,用于填充结构)
- 为什么从std::string的initializer列表中填充std::向量不会调用std::字符串构造函数
- 异常构造函数来填充字符串流
- 使用派生类构造函数填充基类受保护成员
- 尝试在类成员初始化中使用 vector 的填充构造函数失败。怎么了?
- 将字符串的填充构造函数与初始值设定项列表一起使用
- 用构造函数填充向量的C++奇怪行为
- std::initializer_list构造函数除了用值填充一些容器之外,还有什么用
- 在类的构造函数初始值设定项中填充 std::array