类、constexpr 构造函数和 std::string
classes, constexpr constructors, and std::string
我最近一直在使用 constexpr 作为枚举的替代品,但我 运行 遇到了字符串问题(表面上)。
这是我的代码:
namespace Constant {
namespace Implementation {
using std::array;
using std::string;
using type = uint8_t;
namespace Items::Compile_Time {
template<class element, type size>
struct Ray {
const array<element, size> constants {};
constexpr explicit Ray(const array<element, size> constants_in) : constants(constants_in) {}
element constexpr operator[](const type index) const {
return this->constants[index];
}
};
static constexpr type
methods = 0,
signals = 1;
// Plan on cleaning this up later.
constexpr auto BooleanIndicator = Ray<Ray<int, 1>, 2>(array<Ray<int, 1>, 2> {Ray<int, 1> {array<int, 1> {0}}, Ray<int, 1> {array<int, 1> {1}}});
}
}
using namespace Implementation::Items;
}
这很好用。但是,如果我将 'int' 模板参数替换为 'string' 并将字符串放入它们各自的位置(替换最嵌套的 0 和 1),那么我会收到有关具有非平凡析构函数的实例化的错误。
这是为什么?我假设所有数组,包括字符串,都是微不足道的。它们是在堆上分配的吗??我猜如果是这种情况,那么它可能与 const char* 一起使用,但我还没有尝试过(不想,除非我必须)。如果他们确实进入堆,有没有办法强制他们进入堆栈?
未来感谢。
std::string
不是数组。它在堆上分配它的内容(cons SSO),因此在当前的 C++ 中它不能被 constexpr
(有提议使 constexpr
"dynamic" 分配成为可能).
但是,有几种解决方法可以以某种方式使用编译时字符串,例如将它们嵌入到模板参数中 (Foo<'H', 'e', 'l', 'l', 'o'>
)。在大多数情况下,您也可以只使用 const char *
,
我最近一直在使用 constexpr 作为枚举的替代品,但我 运行 遇到了字符串问题(表面上)。
这是我的代码:
namespace Constant {
namespace Implementation {
using std::array;
using std::string;
using type = uint8_t;
namespace Items::Compile_Time {
template<class element, type size>
struct Ray {
const array<element, size> constants {};
constexpr explicit Ray(const array<element, size> constants_in) : constants(constants_in) {}
element constexpr operator[](const type index) const {
return this->constants[index];
}
};
static constexpr type
methods = 0,
signals = 1;
// Plan on cleaning this up later.
constexpr auto BooleanIndicator = Ray<Ray<int, 1>, 2>(array<Ray<int, 1>, 2> {Ray<int, 1> {array<int, 1> {0}}, Ray<int, 1> {array<int, 1> {1}}});
}
}
using namespace Implementation::Items;
}
这很好用。但是,如果我将 'int' 模板参数替换为 'string' 并将字符串放入它们各自的位置(替换最嵌套的 0 和 1),那么我会收到有关具有非平凡析构函数的实例化的错误。
这是为什么?我假设所有数组,包括字符串,都是微不足道的。它们是在堆上分配的吗??我猜如果是这种情况,那么它可能与 const char* 一起使用,但我还没有尝试过(不想,除非我必须)。如果他们确实进入堆,有没有办法强制他们进入堆栈?
未来感谢。
std::string
不是数组。它在堆上分配它的内容(cons SSO),因此在当前的 C++ 中它不能被 constexpr
(有提议使 constexpr
"dynamic" 分配成为可能).
但是,有几种解决方法可以以某种方式使用编译时字符串,例如将它们嵌入到模板参数中 (Foo<'H', 'e', 'l', 'l', 'o'>
)。在大多数情况下,您也可以只使用 const char *
,