Skip to content

Latest commit

 

History

History
50 lines (35 loc) · 1.01 KB

340.md

File metadata and controls

50 lines (35 loc) · 1.01 KB
Info

Example

struct foo {
  auto bar(int v) { return v; }
};

static_assert(42 == std::bind_front<&foo::bar>(foo{}, 42));

Puzzle

  • Can you implement simplified version of std::bind_front with NTTP callables?
// TODO bind_front

struct foo {
    constexpr auto bar(int n) const { return n; }
};

int main() {
    constexpr auto f = foo{};
    constexpr auto fn = bind_front<&foo::bar>(f);
    static_assert(42 == fn(42));
}

https://godbolt.org/z/vKed9Y3a6

Solutions

template<auto Fn>
[[nodiscard]] constexpr auto bind_front(auto&& obj,auto&&...bound_args) {
    return [=](auto&&...args){
        return (obj.*Fn)(bound_args..., args...);
    };
}

https://godbolt.org/z/EExd3rdrx