【发布时间】:2020-04-24 20:41:10
【问题描述】:
我有这样的代码:
template<auto Function>
struct Bind
{
template<typename... Args>
static auto func(Args&&... args)
{
return std::invoke(Function, args...);
}
};
struct F
{
int i;
auto foo(int ii){ i = ii; }
};
int main()
{
F f{};
Bind<&F::foo>::func(f, 5); //set `i` to 5
return Bind<&F::i>::func(f); //return 5
}
但现在我需要添加新函数 int F::foo(); 并且我仍然需要能够从 func 调用这两个函数,例如:
template<typename TBind>
auto bar(F f)
{
TBind::func(f, 5); // calls `void F::foo(int)`
return TBind::func(f); // calls `int F::foo()`
}
是否可以在 C++17 中做到这一点并且仍然使用auto Function?
(C++20 可以有自定义类型作为值模板参数来解决这个问题)
【问题讨论】:
标签: c++ templates c++17 template-meta-programming