模板内本地类成员的名称查找

Name lookup for local class members inside templates

本文关键字:查找 成员      更新时间:2023-10-16

考虑以下代码,它模拟constexpr lambda(建议用于C++17,在C++14中不可用)。

#include <iostream>
template<int M, class Pred>
constexpr auto fun(Pred pred)
{
    return pred(1) <= M;
}
template<int M>
struct C
{
        template<int N>
        static constexpr auto pred(int x) noexcept
        {
            // simulate a constexpr lambda (not allowed in C++14)
            struct lambda
            {
                    int n_, x_;
                    constexpr auto operator()(int y) const noexcept
                    {
                            return this->n_ * this->x_ + y;
                            //     ^^^^       ^^^^ <---- here
                    }
            };
            return fun<M>(lambda{N, x});
        }
};
int main()
{
    constexpr auto res = C<7>::template pred<2>(3);
    std::cout << res; // prints 1, since 2 * 3 + 1 <= 7;
}

此处,lambda在类模板的函数模板成员中定义。令人惊讶的是,我必须this->歧义lambda成员变量n_x_

现场示例(带this->,不带this->

我的印象是,这仅在依赖基类中是必需的,但lambda类只是一个局部类,而不是依赖基类。

问题:有人可以指出我相关的标准语言,以便在模板中查找本地类成员的名称吗?

感谢@dyp的评论,它似乎是 Clang 3.5/3.6 中的一个错误,该错误在 Clang 3.7 的树干尖端中修复。G++ 4.8.1 通过主干尖端也可以正确编译。