3

While there's a lot of stuff floating out there about getting the return type of any templated callback function/method (including lambdas of course), I'm having an extremely hard time finding information about resolving the full call signature of a lambda function. At least in gcc 4.7 it seems to be an edge case where the normal tricks (see below) don't work. Here's what I'm trying to do and have so far (a stripped down version of course)...

template<typename Sig>
struct invokable_type { };

template<typename R, typename...As>
struct invokable_type<R(As...)> {
   static constexpr size_t n = sizeof...(As);
   typedef R(callable_type)(As...);
   template<size_t i>
   struct arg {
       typedef typename peel_type<i, As...> type;
   };
};

peel_type<size_t, typename...> is not included here for brevity but it's a simple argument type peeler (I think there's one built in to C++11, but I never bothered to look). It's unimportant for this question.

Then, of course, specializations (and further properties/typedefs) exist for a myriad of callable types such as R(*)(As...), R(&)(As...), (R(T::*)(As...), std::function<R(As...)>, method cv qualifiers, method lvalue/rvalue qualifiers, etc, etc, etc.

Then, somewhere down the road we have a lovely function or method (function here, doesn't matter) that looks like...

template<typename C, typename...As>
static void do_something(C&& callback, As&&...as) {
    do_something_handler<invokable_type<C>::n, As...>::something(std::forward<C>(callback), std::forward<As>(as)...);
}

Never mind what do_something_handler does... it's entirely immaterial. The problem lies with lambda functions.

For all possible generic invokable signatures I've specialized for (which appears to be all but non-STL functors), this works beautifully when do_something() is called with them as the first argument (template deduction fully works). However, lambda functions result in an uncaptured type signature, resulting in invokable_type<Sig> being used, which means things like ::n and ::args<0>::type simply don't exist.

Not-a-problem example...

void something(int x, int y) {
    return x * y;
}

... and later...

do_something(something, 7, 23);

Problem example...

do_something([](int x, int y) {
        return x * y;
    }, 7, 23);

If I understand lambda functions correctly, the compiler is likely to compile this lambda to a static function within the "namespace" of the defining scope (gcc certainly seems to). For the life of me I can't figure out what the signature actually is though. It looks like it definitely has one that should be deducible via template specialization (based on error reporting).

Another tangential question is even if there is a signature I can use, how cross-compiler dangerous this this? Are lambda compilation signatures standardized or is it all across the board?

14
  • The type of a lambda-expression is a class type (the closure type) with a function call operator. I'm not sure what typedef R(invokable_type)(As...) is, though.
    – dyp
    Commented Oct 3, 2013 at 1:09
  • @DyP: lambda expressions are not explicitly class type. There is no requirement in the C++11 spec for this to be the case and certainly no need if no capturing is done. Please point out the spec if you know this is the case and I stand corrected (and have something to work with, which is good :). As for the typedef, it's the bare typedef for the signature of the function. It's not a pointer or ref function or those would be typedef R(*invokable_type)(As...) and typedef R(&invokable_type)(As...) respectively. Like I said, I specialized for all of that.
    – mr.stobbe
    Commented Oct 3, 2013 at 1:14
  • 1
    [expr.prim.lambda]/3 "The type of the lambda-expression (which is also the type of the closure object) is a unique, unnamed non-union class type — called the closure type"
    – dyp
    Commented Oct 3, 2013 at 1:14
  • Are you then re-using, i.e. hiding, the identifier invokable_type? It's the template name, therefore the injected-class-name. (Also, I think a ; is missing.)
    – dyp
    Commented Oct 3, 2013 at 1:17
  • @DyP: 1) in response to your second response, thanks, I'll dig around that; know how to capture the type? 2) No, invokable_type is not hidden, and, again, even in a simple example works perfectly for all tried non-lambda instances. 3) Probably, I literally typed this from scratch as a conceptual, but not explicitly runnable, example. I'll correct the syntax if that's the case.
    – mr.stobbe
    Commented Oct 3, 2013 at 1:20

2 Answers 2

6

Summing up and extending from the comments:

Per [expr.prim.lambda]/3, the type of a lambda-expression is a class type, just like "ordinary, named function object types":

The type of the lambda-expression (which is also the type of the closure object) is a unique, unnamed non-union class type — called the closure type [...]

Further down, /5 specifies:

The closure type for a lambda-expression has a public inline function call operator (13.5.4) whose parameters and return type are described by the lambda-expression’s parameter-declaration-clause and trailing-return-type respectively. This function call operator is declared const (9.3.1) if and only if the lambda-expression’s parameter-declaration-clause is not followed by mutable. It is neither virtual nor declared volatile. [...]

(it then continues by specifying attributes and exception-specifications)

Which means that the lambda [](int p){ return p/2.0; } behaves in this regard exactly like

struct named_function_object
{
    double operator() (int p) const { return p/2.0; }
};

Therefore, your first specialization

template<typename R, typename...As>
struct invokable_type<R(As...)>;

should already be able to deal with lambdas. The SSCCE

#include <utility>

template<class T>
struct decompose;

template<class Ret, class T, class... Args>
struct decompose<Ret(T::*)(Args...) const>
{
    constexpr static int n = sizeof...(Args);
};

template<class T>
int deduce(T t)
{
    return decompose<decltype(&T::operator())>::n;
}

struct test
{
    void operator() (int) const {}
};

#include <iostream>
int main()
{
    std::cout << deduce(test{}) << std::endl;
    std::cout << deduce([](int){}) << std::endl;
}

compiles fine on recent versions of clang++ and g++. It seems the problem is related to g++4.7


Further research shows that g++-4.7.3 compiles the above example.

The problem might be related to the misconception that a lambda-expression would yield a function type. If we define do_something as

template<class C>
void do_something(C&&)
{
    std::cout << invokable_type<C>::n << std::endl;
}

Then for a call like do_something( [](int){} ), the template parameter C will be deduced to the closure type (no reference), i.e. a class type. The analogous case for the struct test defined above, would be do_something( test{} ), in which case C would be deduced to test.

The specialization of invokable_type that is instantiated is therefore the general case

template<class T>
struct invokable_type;

as T in both cases is not a "composite type" like a pointer or function type. This general case can be used by assuming it only takes a pure class type, and then using the member T::operator() of that class type:

template<class T>
struct invokable_type
{
    constexpr static int n = invokable_type<&T::operator()>::n;
};

or, as Potatoswatter put it, via inheritance

template<class T>
struct invokable_type
    : invokable_type<&T::operator()>
{};

Potatoswatter's version however is more general and probably better, relying on a SFINAE check for the existance of T::operator(), which can provide a better diagnostic message if the operator cannot be found.

N.B. If you prefix a lambda-expression that doesn't capture anything with a unary +, it'll be converted to a pointer-to-function. do_something( +[](int){} ) will work with a specialization invokable_type<Return(*)(Args...)>.

8
  • Le sigh... by the time C++14 is out, C++11 still won't be complete across major compilers/distributions. I'm not complaining about the future though :)
    – mr.stobbe
    Commented Oct 3, 2013 at 1:50
  • How does his code deal with lambdas… I don't see anything that mentions T::operator (). What you need is, given a type, get the signature of its member operator() if any. I'm baffled by all the excessive boilerplate in the Q. As for this answer, you have counted the size of the argument list in T() and didn't touch the lambda. Commented Oct 3, 2013 at 2:01
  • @Potatoswatter: It doesn't in this simple example (as stated in the question), but the problem relates to the fact that you can't do auto fn = std::function([](){}) in gcc 4.7 and properly resolve that without specifying template parameters... that's the underlying problem.
    – mr.stobbe
    Commented Oct 3, 2013 at 2:07
  • @Potatoswatter: And, more to the point, instead of commenting, post a solution that works and tell me why it works. Your argument doesn't work (read the full question with my points about specializing for everything imaginable), but post something that works in 4.7 and explain how/why, and I'll change the vote.
    – mr.stobbe
    Commented Oct 3, 2013 at 2:13
  • @Potatoswatter I've changed my example to make it more useful and deduce the actual arguments from the function object / closure type. I got confused at some point with the T() syntax (there's also an alternative syntax for functions and function pointers, I somehow must have mixed them up).
    – dyp
    Commented Oct 3, 2013 at 2:15
2

As DyP mentions, lambda functors are guaranteed to have a public operator (). Note that operator () cannot be static or non-member.

(A type can also be callable due to the existence of a conversion operator to function pointer type, and stateless lambdas do have such conversion operators, but they still must provide operator ().

You can get the signature of operator () using decltype( & T::operator() ), provided there is only one overload, which is guaranteed for lambdas. This results in a pointer to member function type. You can use a metafunction to strip the T:: part off, or write metafunction queries against the PTMF directly.

#include <iostream>
#include <typeinfo>
#include <type_traits>
#include <tuple>

template< typename t, std::size_t n, typename = void >
struct function_argument_type;

template< typename r, typename ... a, std::size_t n >
struct function_argument_type< r (*)( a ... ), n >
    { typedef typename std::tuple_element< n, std::tuple< a ... > >::type type; };

template< typename r, typename c, typename ... a, std::size_t n >
struct function_argument_type< r (c::*)( a ... ), n >
    : function_argument_type< r (*)( a ... ), n > {};

template< typename r, typename c, typename ... a, std::size_t n >
struct function_argument_type< r (c::*)( a ... ) const, n >
    : function_argument_type< r (c::*)( a ... ), n > {};

template< typename ftor, std::size_t n >
struct function_argument_type< ftor, n,
    typename std::conditional< false, decltype( & ftor::operator () ), void >::type >
    : function_argument_type< decltype( & ftor::operator () ), n > {};


int main() {
    auto x = []( int, long, bool ){};
    std::cout << typeid( function_argument_type< decltype(x), 0 >::type ).name() << '\n';
    std::cout << typeid( function_argument_type< decltype(x), 1 >::type ).name() << '\n';
    std::cout << typeid( function_argument_type< decltype(x), 2 >::type ).name() << '\n';
}

http://coliru.stacked-crooked.com/a/57cd7bb76267ffda

14
  • Can you redo this as an invocation like was the example where constexpr arg count n is passed to another handler where nothing is known about the callback except C (Callable type, C&& in the example)?
    – mr.stobbe
    Commented Oct 3, 2013 at 2:23
  • @mr.stobbe "Can you redo this" is not a polite introduction. I have provided the functionality of invokable_type::arg from your question. If you want to encapsulate it in something else, it's up to you. Commented Oct 3, 2013 at 2:26
  • Well, I don't think that was particularly impolite but I'm sorry if I offended you. It works given, but that's auto x = [lambda], which is not the question at hand. If you replace decltype(x) with decltype([](int, long, bool){}) throughout, it doesn't work.
    – mr.stobbe
    Commented Oct 3, 2013 at 2:32
  • @mr.stobbe The error message (in clang++3.4 and g++4.8.1) should be enlightening ;) "lambda expression in an unevaluated operand". You can deduce it via a function to make it work.
    – dyp
    Commented Oct 3, 2013 at 2:35
  • @Potatoswatter Okay, now extend that to the scenario in the question. That's not very "enlightening". We're not talking about replacing auto x with a direct lambda. We're talking about why std::function([](int i) { }) doesn't resolve to std::function<void(int)>(void(int)&&) or std::function<void(T::&)(int)>(void(T::&)(int)&&)) when it should. I wasn't being verbose about the chain of invocation for nothing.
    – mr.stobbe
    Commented Oct 3, 2013 at 2:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.