Using `std::function` to call non-void function

后端 未结 3 779
一个人的身影
一个人的身影 2020-11-28 15:53

A while ago I used std::function pretty much like this:

std::function func = [](int i) -> int { return i; };
3条回答
  •  时光说笑
    2020-11-28 16:21

    Your code has undefined behavior. It may or may not work as you expect. The reason it has undefined behavior is because of 20.8.11.2.1 [func.wrap.func.con]/p7:

    Requires: F shall be CopyConstructible. f shall be Callable (20.8.11.2) for argument types ArgTypes and return type R.

    For f to be Callable for return type R, f must return something implicitly convertible to the return type of the std::function (void in your case). And int is not implicitly convertible to void.

    I would expect your code to work on most implementations. However on at least one implementation (libc++), it fails to compile:

    test.cpp:7:30: error: no viable conversion from 'int (int)' to 'std::function'
        std::function ff = f;
                                 ^    ~
    

    Ironically the rationale for this behavior stems from another SO question.

    The other question presented a problem with std::function usage. The solution to that problem involved having the implementation enforce the Requires: clause at compile time. In contrast, the solution to this question's problem is forbidding the implementation from enforcing the Requires: clause.

提交回复
热议问题