ラムダをシミュレートする次のコードを検討してくださいconstexpr
(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_
.
実際の例( withthis->
、withoutthis->
)
これは依存する基本クラスでのみ必要であるという印象を受けましたが、そのlambda
クラスは単なるローカル クラスであり、依存する基本クラスではありません。
質問: テンプレート内のローカル クラス メンバーの名前検索に関連する標準を教えてもらえますか?