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

标签 c++ language-lawyer c++14 name-lookup local-class

考虑以下代码,它模拟 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_

实例(with this->without this->)

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

问题:有人可以指点我相关的标准语以查找模板内本地类成员的名称吗?

最佳答案

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

关于c++ - 模板内本地类成员的名称查找,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30819765/

相关文章:

c++ - 使用较小的默认对齐方式重载 operator new

c++ - C++ for Opengl 中未处理的异常

c++ - 将 eigen::MatrixXf 映射到现有的 eigen::matrixXf

c++ - 在放置新数据之前初始化数据是未定义的行为吗?

c++ - std::less 是否应该允许在编译时比较不相关的指针?

c++ - 将通用 Lambda 与 std::find_if 结合使用

c++ - 类型 T 的多个别名

c++ - 转发引用绑定(bind)优先级

c++ - 调用顺序和副作用

c++ - 如何检查数字在 CString 中是否可用 - Visual MFC