考虑以下模拟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_
.
我的印象是这仅在依赖基类中是必需的,但lambda
该类只是一个本地类,而不是依赖基类。
问题:有人可以指出我在模板中查找本地类成员的名称的相关标准吗?