派生类中基类的模板 constexpr 函数出现错误

问题描述 投票:0回答:1
template <typename... Ts>
struct A
{
    template <typename C>
    constexpr auto proc() noexcept { return C{ }; }
    constexpr size_t size() noexcept { return sizeof...(Ts); }
};

template <typename... Ts>
struct B : public A<Ts...>
{
    using base_t = A<Ts...>;

    template <typename... Cs>
    constexpr auto example_prog_1() noexcept
    {
        constexpr size_t tmp = base_t::size();
        // error C2131 : its not constexpr
        return tmp + sizeof...(Cs);
    }

    template <typename... Cs>
    constexpr auto example_prog_0() noexcept
    {
        return example_prog_1<decltype(base_t::template proc<Cs>())...>();
    }
};

int main()
{
    B<int, int, int, int> obj0 { };
    constexpr size_t result0 = obj0.example_prog_0<char, char, char, char>();
}

这不行。 我收到错误 C2131:表达式未求值为常量。

    template <typename... Cs>
    constexpr auto example_prog_1() noexcept
    {
        B dup{ };
        constexpr size_t tmp = dup.size();
        // error none  : its fine
        return tmp + sizeof...(Cs);
    }

但是复制也可以。

添加“此自动自我”并使用它时也可以工作。

有人可以告诉我为什么吗? (msvc /std:c++最新)

c++ c++20 c++23
1个回答
0
投票

base_t::size()
this->base_t::size()

不允许在常量表达式中取消引用在常量表达式外部创建的指针(或可在常量表达式本身中使用的指针)。这将是“读取指针的值”,编译器在编译时并不知道这一点。

这是P2280R4之前的写法,MSVC没有实现。那篇论文和这个问题(Why is a reference argument in a constexpr function not a constant expression?)的答案更详细地解释了为什么之前会发生这种情况。

当您拥有的东西不是指针或引用时(例如,按值

this auto self
B dup {};
),这不再适用。这是一个常量表达式来调用成员函数,因为你不访问对象。

最简单的解决方案是使

size
函数成为
static
。您的很多功能都可以是
static

© www.soinside.com 2019 - 2024. All rights reserved.