静态constexpr成员函数在构造函数中调用它时不会返回constexpr值
我有一个模板类,该类具有一个变异的构造函数,可以对其进行专业化。看起来(基本上):
template<std::size_t SIZE_>
class SomeClass {
public:
static constexpr std::size_t SIZE = SIZE_;
template<typename T>
static constexpr std::size_t computeTotalSize(const T& t) {
return T::SIZE;
}
template <typename T, typename... Ts>
static constexpr std::size_t computeTotalSize(const T& t, const Ts&... ts) {
return computeTotalSize(t) + computeTotalSize(ts...);
}
SomeClass() = default;
template <typename T, typename... Ts>
SomeClass(const T& t, const Ts&... ts) {
static_assert(computeTotalSize(t, ts...) == SOME_NUMBER, "SOME_MESSAGE");
// some other stuff
}
};
现在我尝试实例化someClass&lt; some_number&gt;
:
SomeClass<SOME_NUMBER> someObject((SomeClass<1>()), (SomeClass<2>()), (SomeClass<3>()));
我得到以下错误:
error: constexpr variable 'totalSize' must be initialized by a constant expression
note: function parameter 't' with unknown value cannot be used in a constant expression
我希望这可以正常工作,因为我已经声明了coce> compertotalsize
函数AS constexpr
和所有参数的类型在编译时已知。
我发现,如果我直接调用Computotalsize
函数,则可以正常工作。例如,以下编译:
constexpr std::size_t totalSize = SomeClass<SOME_NUMBER>::computeTotalSize((SomeClass<1>()), (SomeClass<2>()), (SomeClass<3>()));
为什么仅在第二种情况下返回constexpr?我该如何使其在构造函数中工作?
I have a template class which has a variadic constructor that takes specializations of itself. It looks like that (basically):
template<std::size_t SIZE_>
class SomeClass {
public:
static constexpr std::size_t SIZE = SIZE_;
template<typename T>
static constexpr std::size_t computeTotalSize(const T& t) {
return T::SIZE;
}
template <typename T, typename... Ts>
static constexpr std::size_t computeTotalSize(const T& t, const Ts&... ts) {
return computeTotalSize(t) + computeTotalSize(ts...);
}
SomeClass() = default;
template <typename T, typename... Ts>
SomeClass(const T& t, const Ts&... ts) {
static_assert(computeTotalSize(t, ts...) == SOME_NUMBER, "SOME_MESSAGE");
// some other stuff
}
};
Now I try to instantiate SomeClass<SOME_NUMBER>
:
SomeClass<SOME_NUMBER> someObject((SomeClass<1>()), (SomeClass<2>()), (SomeClass<3>()));
I get the following errors:
error: constexpr variable 'totalSize' must be initialized by a constant expression
note: function parameter 't' with unknown value cannot be used in a constant expression
I expected this to work fine since I have declared computeTotalSize
function as constexpr
and the type of all parameters is known at compile time.
I found out that if I call computeTotalSize
function directly, it works fine. For example, the following compiles:
constexpr std::size_t totalSize = SomeClass<SOME_NUMBER>::computeTotalSize((SomeClass<1>()), (SomeClass<2>()), (SomeClass<3>()));
Why is it returning a constexpr in the second case only? How can I make it work in the constructor?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是,即使模板参数命名
t
是A 常数表达式函数参数命名t
isn' t。而且,由于
t
不是常数表达式,它不能在您试图在错误中使用的constexpr上下文中使用:The problem is that even though the template parameter named
T
is a constant expression the function parameter namedt
isn't.And since
t
isn't a constant expression it cannot be used in the constexpr context you're trying to use it in as mentioned in the error saying: