llvm / llvm/llvm-project

[libc++] std::function move constructor doesn't compile with `-D_LIBCPP_REMOVE_TRANSITIVE_INCLUDES`

Open
#170,123 0 comments 0 reactions 0 assignees View on GitHub
libc++
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

https://godbolt.org/z/cnonfeW8e

```cpp
#include

void test() {
std::function f = [] { };
}
```
Compiling with `-std=c++14 -stdlib=libc++ -D_LIBCPP_REMOVE_TRANSITIVE_INCLUDES` fails:
```
In file included from :1:
In file included from /cefs/ed/ed52bd7ae910fa279dd74bf8_clang-trunk-20251201/bin/../include/c++/v1/functional:553:
/cefs/ed/ed52bd7ae910fa279dd74bf8_clang-trunk-20251201/bin/../include/c++/v1/__functional/function.h:211:14: error: no matching 'operator new' function for non-allocating placement new expression; include
211 | __f_ = ::new (std::addressof(__buf_)) _Fun(std::move(__f));
| ^~
/cefs/ed/ed52bd7ae910fa279dd74bf8_clang-trunk-20251201/bin/../include/c++/v1/__functional/function.h:711:50: note: in instantiation of function template specialization 'std::__function::__value_func::__value_func<(lambda at :4:35), 0>' requested here
711 | function<_Rp(_ArgTypes...)>::function(_Fp __f) : __f_(std::move(__f)) {}
| ^
:4:35: note: in instantiation of function template specialization 'std::function::function<(lambda at :4:35), void>' requested here
4 | std::function f = [] { };
| ^
1 error generated.
Compiler returned: 1
```
Adding `#include ` as the error message suggests does of course make the compilation error go away. However, I don't think this is expected behavior - my understanding is that the `_LIBCPP_REMOVE_TRANSITIVE_INCLUDES` definition will only remove _unnecessary_ transitive includes.

Contributor guide

Open the contributing guide

Research direction

Start with libc++'s __functional/function.h, particularly the placement-new use shown in the compiler diagnostic, and reproduce the issue with the provided std::function example and flags. Done means the example compiles under C++14 with -stdlib=libc++ and -D_LIBCPP_REMOVE_TRANSITIVE_INCLUDES without requiring an explicit include.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.