基类中的多维下标运算符不参与推导此的重载解析

问题描述 投票:0回答:1

示例在这里(上帝螺栓)

我的基类中有一个多维下标运算符的默认实现

subscript
。创建派生
t0
以提供下标运算符。

    struct subscript {
        constexpr decltype(auto) operator[](
            this auto&& t, auto&& first_arg,
            auto&& second_arg) noexcept(noexcept(t[first_arg][second_arg]))
            requires requires { t[first_arg][second_arg]; }
        {
            return (t)[first_arg][second_arg];
        }
    };

    struct t0 : subscript {
        auto operator[](const int) const { return *this; }
    };

    auto f0(t0 v) {
        return v[0, 1];  // no
    }

在f0函数中调用t0的多维下标。 Clang 无法编译并显示以下消息:

:44:13:错误:类型“t0”没有可行的重载运算符[]
44 | 44返回 v[0, 1]; // 不
:20:10:注意:候选函数不可行:需要 1 个参数,但提供了 2 个参数
20 |自动运算符[](const int) const { return *this; }

Clang 似乎没有将基类operator[] 进行重载。但在f1中显式调用基类operator[],效果很好。

在另一种情况下,t1 将运算符[]内联到类本身中。效果很好。

    struct t1 {
        auto operator[](const int) const { return *this; }

        constexpr decltype(auto) operator[](
            this auto&& t, auto&& first_arg,
            auto&& second_arg) noexcept(noexcept(t[first_arg][second_arg]))
            requires requires { t[first_arg][second_arg]; }
        {
            return (t)[first_arg][second_arg];
        }
    };

    auto f2(t1 v) {
        return v[0, 1];  // yes
    }

问题是为什么Clang无法编译第一个案例?如何修复它以正确调用基类中的运算符 [] 而无需明确指定?

c++ inheritance operator-overloading c++23 explicit-object-parameter
1个回答
0
投票

我只是注意到我需要添加

using subscript::operator[];

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