mardi 22 août 2017

Is the call operator of lambdas required to have linkage?

I have some code that deal with address of functions, lambdas and template arguments.

Here's a snippet that shows to me some usage of these address to functions:

template<typename> void foo();

template<typename T>
struct Test {
    // Error might happen here! if &T::operator() has no linkage
    using type = std::integral_constant<decltype(&T::operator()), &T::operator()>;

    void call_foo() {
        foo<type>();
    }
};

The thing is, for being able to send &T::operator() as template argument, the call operator must have linkage. So code like this:

int main() {
    auto l = []{};

    Test<decltype(l)> test; // okay in my case, just lucky?
    test.call_foo();

    struct {
        void operator() {}
    } bar;

    Test<decltype(bar)> test2; // not okay, operator() has no linkage
    test2.call_foo();
}

Works in my case. Maybe I'm just lucky? If the call operator of the lambda is defined in the function, it has no linkage, making the alias type ill-formed. Is the call operator of the lambda required to have linkage?

Aucun commentaire:

Enregistrer un commentaire