![](/img/trans.png)
[英]initialize an array of compile-time constant size with incrementing numbers
[英]Initialize array of compile time defined size as constant expression
我有一个字符串数组,必须分配一次,它们的底层c_str
必须在程序的整个持续时间内保持有效。
有一些API提供有关某些任意数据类型的信息。 看起来像这样:
// Defined outside my code
#define NUMBER_OF_TYPES 23
const char* getTypeSuffix(int index);
getTypeSuffix
不是constexpr
,因此必须至少部分在运行时工作。
我必须提供的界面:
// Returned pointer must statically allocated (not on stack, not malloc)
const char* getReadableTypeName(int type);
现在我的数组应该有以下类型:
std::string typeNames[NUMBER_OF_TYPES];
为了我的目的,它将在一个包装类中初始化,就在构造函数中:
class MyNames
{
MyNames()
{
for (int i = 0; i < NUMBER_OF_TYPES; ++i)
{
names[i] = std::string("Type ") + getTypeSuffix(i);
}
}
const char* operator[](int type) { return _names[(int)type].c_str(); }
private:
std::string _names[NUMBER_OF_TYPES];
};
然后以单一方式使用它,例如:
const char* getReadableTypeName(int type)
{
static MyNames names;
return names[type];
}
现在我要改进的是,我可以看到构造函数中的for循环可以替换为:
MyNames() : _names{std::string("Type ") + getTypeSuffix(0), std::string("Type ") + getTypeSuffix(1), ... , std::string("Type ") + getTypeSuffix(NUMBER_OF_TYPES-1)}
{}
显然是伪代码,但是你得到了重点 - 数组可以直接初始化,使构造函数没有正确,这是整洁的。 它还意味着数组成员_names
可以是const
,进一步强制正确使用此帮助程序类。
我很确定在编译时通过表达式填充数组会有很多其他用途,而不是循环。 我甚至怀疑这是03
期间发生的事情。
有没有办法编写一个C ++ 11样式的数组初始化列表,它具有灵活的长度并由表达式定义? 另一个简单的例子是:
constexpr int numberCount = 10;
std::string numbers[] = {std::to_string(1), std::to_string(2), ... , std::to_string(numberCount)};
同样,表达式而不是循环。
我不是在问这个问题,因为我试图大幅度提高性能,但是因为我想学习C ++ 14及更高版本的新的,简洁的功能。
而不是C数组使用std::array
,那么你可以编写函数来返回std::array
,然后你的成员可以是const
:
std::array<std::string, NUMBER_OF_TYPES> build_names()
{
std::array<std::string, NUMBER_OF_TYPES> names;
for (int i = 0; i < NUMBER_OF_TYPES; ++i)
{
names[i] = std::string("Type ") + getTypeSuffix(i);
}
return names;
}
class MyNames
{
MyNames() : _names(build_names()) {}
const char* operator[](int type) const { return _names[(int)type].c_str(); }
private:
const std::array<std::string, NUMBER_OF_TYPES> _names;
};
现在你有了std::array
,你可以使用variadic模板而不是loop,比如( std::index_sequence
stuff是C ++ 14,但是可以在C ++ 11中实现):
template <std::size_t ... Is>
std::array<std::string, sizeof...(Is)> build_names(std::index_sequence<Is...>)
{
return {{ std::string("Type ") + getTypeSuffix(i) }};
}
然后调用它:
MyNames() : _names(build_names(std::make_index_sequence<NUMBER_OF_TYPES>())) {}
您可以按照初始化函数:
std::array<std::string, NUMBER_OF_TYPES> initializeNames()
{
std::array<std::string, NUMBER_OF_TYPES> names;
for (int i = 0; i < NUMBER_OF_TYPES; ++i) {
names[i] = std::string("Type ") + getTypeSuffix(i);
}
return names;
}
const char* getReadableTypeName(int type)
{
static auto const names = initializeNames();
return names[type].c_str();
}
这可以是一个立即调用的lambda:
static auto const names = []{
std::array<std::string, NUMBER_OF_TYPES> names;
// ...
return names;
}();
或者你真的需要array
要求吗? 无论如何我们正在制作字符串所以我不明白,那么你可以使用range-v3:
char const* getReadableTypeName(int type) {
static auto const names =
view::iota(0, NUMBER_OF_TYPES)
| view::transform([](int i){ return "Type "s + getTypeSuffix(i); })
| ranges::to<std::vector>();
return names[type].c_str():
}
您可以在C ++ 14中使用std::make_integer_sequence
和委托构造函数(C ++ 11中存在std::make_integer_sequence
实现,因此这不是特定的C ++ 14)来获取整数的模板参数包
#include <string>
#include <utility>
#define NUMBER_OF_TYPES 23
const char* getTypeSuffix(int index);
class MyNames
{
MyNames() : MyNames(std::make_integer_sequence<int, NUMBER_OF_TYPES>{}) {}
template<int... Indices>
MyNames(std::integer_sequence<int, Indices...>) : _names{ (std::string("Type ") + getTypeSuffix(Indices))... } {}
const char* operator[](int type) { return _names[(int)type].c_str(); }
private:
const std::string _names[NUMBER_OF_TYPES];
};
这意味着没有默认构造的字符串。
既然你想要使用新功能,那么让我们使用range-v3
(即将成为C ++ 2a中的ranges
库)来编写一些非常简短的代码:
const char* getReadableTypeName(int type)
{
static const std::vector<std::string> names =
view::ints(0, 23) | view::transform([](int i) {
return "Type " + std::to_string(i);
});
return names[type].c_str();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.