Index: include/llvm/ADT/STLExtras.h =================================================================== --- include/llvm/ADT/STLExtras.h +++ include/llvm/ADT/STLExtras.h @@ -135,6 +135,26 @@ operator bool() const { return callback; } }; +/// A simple functor that just calls a function specified at compile time. +/// Unlike storing a function pointer, this has zero runtime overhead because +/// the function address is part of the object type. +/// +/// Example usage: +/// void f(); +/// +/// static_fptr obj; +/// obj(); +template::value>::type> +struct static_fptr { + template + auto operator()(Args &&... args) const + noexcept(noexcept(Fn(std::forward(args)...))) + -> decltype(Fn(std::forward(args)...)) { + return Fn(std::forward(args)...); + } +}; + // deleter - Very very very simple method that is used to invoke operator // delete on something. It is used like this: // Index: unittests/ADT/STLExtrasTest.cpp =================================================================== --- unittests/ADT/STLExtrasTest.cpp +++ unittests/ADT/STLExtrasTest.cpp @@ -364,4 +364,21 @@ EXPECT_EQ(5, count); } +int g() { return 42; } +void g(int &&) { } + +TEST(STLExtrasTest, static_fptr) { + { + static_fptr obj; + EXPECT_EQ(obj(), 42); + } + + // Check for perfect forwarding of the parameters. + { + static_fptr obj; + int i = 0; + obj(std::move(i)); + } +} + } // namespace