remove copying when invoking objects stored in sbo#24
remove copying when invoking objects stored in sbo#24pdimov merged 1 commit intoboostorg:developfrom
Conversation
|
Instead of |
I'm trying that but I'm having trouble getting code like this to then compile: move_only_function<int( noex_callable ) const> f2( std::move( f1 ) );
BOOST_TEST_EQ( f2( c ), 1235 );
move_only_function<int( noex_callable )> f3( std::move( f2 ) );
BOOST_TEST_EQ( f3( c ), 1235 );The compilers reject it with: The problem being there's no conversion from: int (*)(const boost::compat::detail::storage&to int (*)(boost::compat::detail::storage&Many of the tests fail like: Do you want me to push up my WIP branch? The more I think about it, the const_cast is probably harmless here anyway because we correctly apply the cv-ref qualifiers at the invoke site. I'm not sure what we should do to fix these kinds of function pointer cast errors. |
|
Ah, I see. Fewer const_casts will be needed if you declare the functions to take |
eed8de3 to
d9faeab
Compare
test/move_only_function_test.cpp
Outdated
| #endif | ||
|
|
||
| #ifdef _MSC_VER | ||
| #pragma warning(disable: 4789) // false buffer overrun warning in test_mutable_lambda() |
There was a problem hiding this comment.
What is the buffer overrun warning?
|
Can you please structure this as: commit 1 - test additions without the 4789 warning suppression; commit 2 - changes to the implementation; commit 3 - warning suppression in the test? |
d9faeab to
01d7279
Compare
|
I've merged the first two commits to develop. As for the third one, the code is correct, the branch is never taken. However, there's still a better fix because the reason for the warning is that the compile-time constant condition
A drive-by comment: Best practices when using placement new is to qualify it, |
Currently, the code branches on whether to use SBO or not using a runtime check, i.e. a raw `if(x)`. This can trick the msvc optimizer into believing the SBO path can be taken, which in the case of sufficiently large Callables triggers a buffer overrun warning as emplacing into the small buffer would exceed its size. By updating the code to instead dispatch at compile-time via tags, the msvc optimizer is no longer confused and the warning disappears as the code path is now truly unreachable.
01d7279 to
4596e89
Compare
Resolves: #23
Right now the invoke holders take the storage by-value which is strictly incorrect when using SBO as it creates a copy of the callable which leads to surprising results in user-code that expect multiple calls to a lambda with a mutable capture.