方法中使用的静态模板成员数组sizeof
Static template member array sizeof used in method
我有一个模板类,它有一个静态成员数组和一个需要数组大小的方法。
template <int i>
struct Foo {
static int data[];
static int size() {
return sizeof(data) / sizeof(data[0]);
}
};
我想为每个模板特化初始化数组。
template <>
int Foo<0>::data[] = { 0, 1, 2 };
只要我只在一个cpp文件中使用它就可以工作。但是如何在多个文件中使用它呢?
如果我把初始化的头,链接将失败,因为:
multiple definition of `Foo<0>::data'
如果我把它放到一个cpp文件中,其他的将无法编译,因为:
invalid application of ‘sizeof’ to incomplete type ‘int []’
我对不将数组更改为std::vector的解决方案感兴趣。
如果你把struct模板定义留在头文件中,你可能会在初始化数据的翻译单元中强制模板实例化,并使用extern来阻止它,例如:
// imp.cc: initialization and instantiation
template <>
int Foo<0>::data[] = { 0, 1, 2 };
template struct Foo<0>;
// main.cc: extern declaration and usage:
template<> extern int Foo<0>::size ();
... Foo<0>::size () ...
(我用一个小例子和clang进行了测试)
这个适合我:
foo:
#ifndef FOO_H
#define FOO_H
template <int i>
struct Foo {
static int data[];
static int size() {
return sizeof(data) / sizeof(data[0]);
}
};
#endif
foo - 0. h:
#ifndef FOO_0_H
#define FOO_0_H
#include "Foo.h"
// Provide declarations of the members for 0
template <> int Foo<0>::data[];
template <> int Foo<0>::size();
#endif
foo - 0. - cpp:
#include "Foo-0.h"
// Define the members for 0
template <> int Foo<0>::data[] = {10, 20, 30};
template <> int Foo<0>::size()
{
return sizeof(data) / sizeof(data[0]);
}
main.cpp:
#include <iostream>
#include "Foo-0.h"
int main()
{
std::cout << Foo<0>::data[0] << std::endl;
std::cout << Foo<0>::size() << std::endl;
};
输出:10
3
您可以尝试将其封装在未命名的名称空间中,这将提供内部链接并绕过' Foo<0>::data'的多个定义
namespace{
template <int i>
struct Foo {
static int data[];
static int size() {
return sizeof(data) / sizeof(data[0]);
}
};
}
相关文章:
- 是否可以将结构数组别名为结构成员数组?
- C++:初始化 constexpr 构造函数中的成员数组
- 使用带有参数包的数组的成员数组初始化类
- C++ 在析构函数调用之前删除的动态成员数组
- 初始化在类类型 #define 中定义的非静态成员数组,不带默认 ctor
- 指向具有不同大小的成员数组的指针
- 用另一个 constexpr 数组对成员数组进行大括号初始化
- 对象成员数组C++默认初始化
- 如何在构造函数中的类成员数组中分配数组值,而无需使用STD库
- 将类成员数组组合到单个数组时性能下降
- 如何将数据从公共方法添加到同一类中的私有成员数组中?
- 如何在C 中的派生类中分配数据成员数组
- C 集合构建器中类成员数组的2D数组大小
- 初始化不可复制、不可移动、显式构造类型的成员数组
- C 指针可以指向字符串文字的静态成员数组
- 成员数组位于何处
- 将对象成员数组与字符串进行比较
- 如何在 c++ 中从对象数组中获取数据成员数组
- 声明模板会员参考另一个类的模板成员数组
- C 初始化成员数组