Name lookup for local class members inside templates
Asked Answered
G

1

6

Consider the following code, that simulates a constexpr lambda (proposed for C++17, not available in 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;
}

Here, the lambda is defined inside a function template member of a class template. Surprisingly, I have to this->ambiguate the lambda member variables n_ and x_.

Live examples (with this->, without this->)

I was under the impression that this is only necessary in dependent base classes, but the lambda class is just a local class, not a dependent base class.

Question: can someone point me to the relevant Standardese for name lookup of local class members inside templates?

Gainless answered 13/6, 2015 at 14:29 Comment(1)
The code w/o this is accepted by clang++3.7 (ToT). See e.g. melpon.org/wandboxLeprosarium
G
2

Thanks to the comment of @dyp, it appears to be a bug in Clang 3.5 / 3.6 that is fixed in Clang 3.7 tip of trunk. G++ 4.8.1 through tip of trunk compile this correctly as well.

Gainless answered 13/6, 2015 at 15:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.