当用作非类型模板参数时,是否可以自动派生 std::array 的大小
Can the size of an std::array be automatically derived when used as a non-type template parameter
以下作品。但是,是否可以省略 SIZE 模板参数(即不一定是数组的 SIZE 模板参数(?
template <
class T,
size_t SIZE,
std::array<T, SIZE> & ARR
>
class Foo{};
换句话说,编译器是否可以从数组模板参数中推断出 SIZE?从而消除界限
const size_t SIZE,
使用 C++17 您可以使用自动模板参数:
template<auto&>
struct Foo {};
然后,您可以发送对全局的引用:
void test() {
static auto arr = std::array{1, 2, 3, 4, 5}; // no linkage works in C++17
Foo<arr> f{};
}
您可以在编译时通过std::tuple_size(std::array)
获取大小(自 C++11 起(,例如
template <
class ARRAY
>
struct Foo{
static constexpr size_t SIZE = std::tuple_size<ARRAY>::value;
using T = typename ARRAY::value_type;
};
并将其用作
Foo<array<int, 3>> f;
相关文章:
- 绑定派生类方法C++从实例范围之外的分隔 std::function 变量调用
- 使用 std::variant<...时调用 BaseState 函数而不是派生函数>
- 使用 std::enable_if 限制派生类的模板参数时出现编译错误
- 指向基类派生类的 std::unique_ptr 的指针
- "std::shared_from_this"不能由其派生类继承吗?
- C++派生类重载函数(带有 std::function 参数)不可见
- C++ 事件管理器的回调,使用 std::function 和 std:bind 以及派生类作为参数
- 从组件的 std::type_index 获取派生最多的类型
- std::数组派生类聚合初始化
- 如何使用派生类类型数据初始化 std::shared_ptr?
- 自定义派生的 std::exception 类的 'what' 函数返回神秘的废话
- 抽象和派生与std::list相结合
- std::用派生类对象设置,但用gcc8.1设置基类比较器
- 派生自 std::exception 的类的赋值运算符
- 在 MSVC 中,从 std::string_view 派生的对象比较不明确
- 在从仅移动类型派生的类中定义析构函数在使用 std::vector emplace_back或push_back创建时会
- 派生 std::ofstream 和重载运算符<<
- 如何将 std::list of 派生类而不是 std::list of parent class 传递给函数
- 当用作非类型模板参数时,是否可以自动派生 std::array 的大小
- 无法通过引用 std::exception 来捕获从 std::exception 派生的类