Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::bind on a generic lambda - auto type deduction

Tags:

c++

c++14

Consider the following code:

#include <iostream>
#include <functional>

int main() {
    auto run = [](auto&& f, auto&& arg) {
        f(std::forward<decltype(arg)>(arg));
    };
    auto foo = [](int &x) {};
    int var;
    auto run_foo = std::bind(run, foo, var);
    run_foo();
    return 0;
}

Which gives the following compilation error when compiled with clang:

$ clang++ -std=c++14 my_test.cpp

my_test.cpp:6:9: error: no matching function for call to object of type 'const (lambda at my_test.cpp:8:16)'
        f(std::forward<decltype(arg)>(arg));
        ^
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.3.1/../../../../include/c++/6.3.1/functional:998:14: note: in instantiation of function template specialization 'main()::(anonymous class)::operator()<const (lambda at my_test.cpp:8:16) &, const int &>' requested here
        = decltype( std::declval<typename enable_if<(sizeof...(_Args) >= 0),
                    ^
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.3.1/../../../../include/c++/6.3.1/functional:1003:2: note: in instantiation of default argument for 'operator()<>' required here
        operator()(_Args&&... __args) const
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
my_test.cpp:11:12: note: while substituting deduced template arguments into function template 'operator()' [with _Args = <>, _Result = (no value)]
    run_foo();
           ^
my_test.cpp:8:16: note: candidate function not viable: 1st argument ('const int') would lose const qualifier
    auto foo = [](int &x) {};
               ^
my_test.cpp:8:16: note: conversion candidate of type 'void (*)(int &)'
1 error generated.

Why is arg deduced to be const int& instead of just int&?

std::bind documentation says:

Given an object g obtained from an earlier call to bind, when it is invoked in a function call expression g(u1, u2, ... uM), an invocation of the stored object takes place, as if by std::invoke(fd, std::forward(v1), std::forward(v2), ..., std::forward(vN)), where fd is a value of type std::decay_t the values and types of the bound arguments v1, v2, ..., vN are determined as specified below.

...

Otherwise, the ordinary stored argument arg is passed to the invokable object as lvalue argument: the argument vn in the std::invoke call above is simply arg and the corresponding type Vn is T cv &, where cv is the same cv-qualification as that of g.

But in this case, run_foo is cv-unqualified. What am I missing?

like image 778
Kinan Al Sarmini Avatar asked Apr 29 '17 06:04

Kinan Al Sarmini


1 Answers

MWE:

#include <functional>

int main() {
    int i;
    std::bind([] (auto& x) {x = 1;}, i)();
}

[func.bind]/(10.4) states that the cv-qualifiers of the argument passed to the lambda are those of the argument to bind, augmented by the cv-qualifiers of the call wrapper; but there are none, and thus a non-const int should be passed in.

Both libc++ and libstdc++ fail to resolve the call. For libc++, reported as #32856, libstdc++ as #80564. The main problem is that both libraries infer the return type in the signature somehow, looking like this for libstdc++:

  // Call as const
template<typename... _Args, typename _Result
  = decltype( std::declval<typename enable_if<(sizeof...(_Args) >= 0),
           typename add_const<_Functor>::type&>::type>()(
               _Mu<_Bound_args>()( std::declval<const _Bound_args&>(),
                                   std::declval<tuple<_Args...>&>() )... ) )>
_Result operator()(_Args&&... __args) const 

During template argument deduction as necessitated by overload resolution, the default template argument will be instantiated, which causes a hard error due to our ill-formed assignment inside the closure.

This can be fixed by perhaps a deduced placeholder: remove _Result and its default argument entirely, and declare the return type as decltype(auto). This way, we also get rid of SFINAE which influences overload resolution and thereby induces incorrect behaviour:

#include <functional>
#include <type_traits>

struct A {
  template <typename T>
  std::enable_if_t<std::is_const<T>{}> operator()(T&) const;
};

int main() {
    int i;
    std::bind(A{}, i)();
} 

This should not compile—as explained above, the argument passed to A::operator() should be non-const because i and the forwarding call wrapper are. However, again, this compiles under libc++ and libstdc++, because their operator()s fall back on const versions after the non-const ones fail under SFINAE.

like image 60
Columbo Avatar answered Nov 03 '22 23:11

Columbo