静态constexpr成员函数在构造函数中调用它时不会返回constexpr值

发布于 2025-02-04 11:47:04 字数 1580 浏览 2 评论 0原文

我有一个模板类,该类具有一个变异的构造函数,可以对其进行专业化。看起来(基本上):

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迟月 2025-02-11 11:47:04

问题是,即使模板参数命名t是A 常数表达式函数参数命名t isn' t。

而且,由于t不是常数表达式,它不能在您试图在错误中使用的constexpr上下文中使用:

function parameter 't' with unknown value cannot be used in a constant expression

The problem is that even though the template parameter named T is a constant expression the function parameter named t 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:

function parameter 't' with unknown value cannot be used in a constant expression
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文