6

ラムダをシミュレートする次のコードを検討してください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クラスは単なるローカル クラスであり、依存する基本クラスではありません。

質問: テンプレート内のローカル クラス メンバーの名前検索に関連する標準を教えてもらえますか?

4

1 に答える 1

2

@dyp さんのコメントのおかげで、トランクのClang 3.7チップで修正された Clang 3.5 / 3.6 のバグのようです。G++ 4.8.1 からトランクのヒントまで、これも正しくコンパイルされます。

于 2015-06-13T15:26:32.947 に答える