我们考虑使用以下仿函数来帮助检测其struct functor
{
void operator()() {std::cout << "functor::operator" << std::endl;}
void operator()() const {std::cout << "functor::operator const" << std::endl;}
void operator()() volatile {std::cout << "functor::operator volatile" << std::endl;}
void operator()() const volatile {std::cout << "functor::operator const volatile" << std::endl;}
void operator()(int) & {std::cout << "functor::operator &" << std::endl;}
void operator()(int) const& {std::cout << "functor::operator const&" << std::endl;}
void operator()(int) volatile& {std::cout << "functor::operator volatile&" << std::endl;}
void operator()(int) const volatile& {std::cout << "functor::operator const volatile&" << std::endl;}
void operator()(int) && {std::cout << "functor::operator &&" << std::endl;}
void operator()(int) const&& {std::cout << "functor::operator const&&" << std::endl;}
void operator()(int) volatile&& {std::cout << "functor::operator volatile&&" << std::endl;}
void operator()(int) const volatile&& {std::cout << "const volatile&&" << std::endl;}
};
的版本。
C++17
我想知道,在wrapper1
中(如果可行的话)如何编写两个函数包装器,将函数存储在元组中并使用https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md:
wrapper2
wrapper1 w1(functor{});
wrapper2 w2(functor{});
w1(0); // should call the && version since the functor was built as a temporary
w2(0); // should call the & version since the wrapper was built as a value
例如:
template <class F>
struct wrapper_a
{
constexpr wrapper_a(F f) noexcept: _tuple(f) {}
template <class... Args>
void operator()(Args&&... args)
{std::get<0>(_tuple)(std::forward<Args>(args)...);}
std::tuple<F> _tuple;
};
template <class F>
struct wrapper_b
{
template <class G>
constexpr wrapper_b(G&& g) noexcept: _tuple(std::forward<G>(g)) {}
template <class... Args>
void operator()(Args&&... args)
{std::get<0>(_tuple)(std::forward<Args>(args)...);}
std::tuple<F> _tuple;
};
template <class F> wrapper_b(F&& f) -> wrapper_b<F>;
template <class F>
struct wrapper_c
{
template <class G>
constexpr wrapper_c(G&& g) noexcept: _tuple(std::forward<G>(g)) {}
template <class... Args>
void operator()(Args&&... args)
{std::forward<F>(std::get<0>(_tuple))(std::forward<Args>(args)...);}
std::tuple<F> _tuple;
};
template <class F> wrapper_c(F&& f) -> wrapper_c<F>;
以下是我正在寻找的事情的一些非常粗略的探索。注意:这只是为了“品味”我的想法。
{{1}}
如何实现我的目标?这甚至可能吗?
答案 0 :(得分:1)
不确定理解你的确切要求......而且我是一个参考转发的新手......无论如何
第一个将根据函子的限定符调用运算符的wrapper1
也许专家可以避免这种并发症,但在我看来,你需要两个模板参数
template <typename F, typename G>
struct wrapper1
其中F
是传递给构造函数的参数副本的类型,而G
是预期的类型。
所以你有一个F
值
F f;
您可以通过转发
使用它template <typename ... Args>
void operator() (Args && ... as) &
{ std::forward<G>(f)(std::forward<Args>(as)...); }
为简化起见,您可以按如下方式定义模板推论
template <typename F>
wrapper1(F && f) -> wrapper1<std::decay_t<F>, decltype(std::forward<F>(f))>;
第二个包装器2将根据包装器的限定符调用操作符
也许我错了,但在我看来这更简单。
您只需要一个模板参数
template <typename F>
struct wrapper2
您只需在r-reference运算符中使用std::move()
template <typename ... Args>
void operator() (Args && ... as) &&
{ std::move(f)(std::forward<Args>(as)...); }
演绎指南很简单
template <typename F>
wrapper2(F && f) -> wrapper2<std::decay_t<F>>;
以下是有限的(const
/不 - const
和l值/ r值参考替代方案),但是完整的工作示例。
观察到由constness问题导致的一些编译错误(如果使用not const wrapper1
可调用初始化const
,则存在问题)
#include <iostream>
struct functor
{
void operator()(int) &
{std::cout << "functor::operator &" << std::endl;}
void operator()(int) const &
{std::cout << "functor::operator const &" << std::endl;}
void operator()(int) &&
{std::cout << "functor::operator &&" << std::endl;}
void operator()(int) const &&
{std::cout << "functor::operator const &&" << std::endl;}
};
template <typename F, typename G>
struct wrapper1
{
template <typename H>
constexpr wrapper1 (H && f0) noexcept : f{std::forward<H>(f0)}
{}
template <typename ... Args>
void operator() (Args && ... as) &
{ std::forward<G>(f)(std::forward<Args>(as)...); }
template <typename ... Args>
void operator() (Args && ... as) const &
{ std::forward<G>(f)(std::forward<Args>(as)...); }
template <typename ... Args>
void operator() (Args && ... as) &&
{ std::forward<G>(f)(std::forward<Args>(as)...); }
template <typename ... Args>
void operator() (Args && ... as) const &&
{ std::forward<G>(f)(std::forward<Args>(as)...); }
F f;
};
template <typename F>
wrapper1(F && f)
-> wrapper1<std::decay_t<F>, decltype(std::forward<F>(f))>;
template <typename F>
struct wrapper2
{
template <typename H>
constexpr wrapper2 (H && f0) noexcept : f{std::forward<H>(f0)}
{}
template <typename ... Args>
void operator() (Args && ... as) &
{ f(std::forward<Args>(as)...); }
template <typename ... Args>
void operator() (Args && ... as) const &
{ f(std::forward<Args>(as)...); }
template <typename ... Args>
void operator() (Args && ... as) &&
{ std::move(f)(std::forward<Args>(as)...); }
template <typename ... Args>
void operator() (Args && ... as) const &&
{ std::move(f)(std::forward<Args>(as)...); }
F f;
};
template <typename F>
wrapper2(F && f) -> wrapper2<std::decay_t<F>>;
int main ()
{
functor fc;
functor const fd;
wrapper1 w1a{functor{}};
wrapper1 w1b{static_cast<functor const &&>(functor{})};
wrapper1 w1c{fc};
wrapper1 w1d{fd};
wrapper1 const w1e{functor{}};
wrapper1 const w1f{static_cast<functor const &&>(functor{})};
wrapper1 const w1g{fc};
wrapper1 const w1h{fd};
w1a(0);
w1b(0);
w1c(0);
w1d(0);
std::cout << "----------------------------" << std::endl;
// w1e(0); // compilation error
w1f(0);
// w1g(0); // compilation error
w1h(0);
std::cout << "----------------------------" << std::endl;
wrapper1<functor, functor&&>{functor{}}(0);
wrapper1<functor, functor const &&>
{static_cast<functor const &&>(functor{})}(0);
wrapper1<functor, functor&>{fc}(0);
wrapper1<functor, functor const &>{fd}(0);
std::cout << "----------------------------" << std::endl;
// (wrapper1 <functor, functor&&> const)
//{functor{}}(0); // compilation error
(wrapper1<functor, functor const &&> const)
{static_cast<functor const &&>(functor{})}(0);
// (wrapper1<functor, functor&> const){fc}(0); // compilation error
(wrapper1<functor, functor const &> const){fd}(0);
wrapper2 w2a{functor{}};
wrapper2 w2b{static_cast<functor const &&>(functor{})};
wrapper2 w2c{fc};
wrapper2 w2d{fd};
wrapper2 const w2e{functor{}};
wrapper2 const w2f{static_cast<functor const &&>(functor{})};
wrapper2 const w2g{fc};
wrapper2 const w2h{fd};
std::cout << "----------------------------" << std::endl;
w2a(0);
w2b(0);
w2c(0);
w2d(0);
std::cout << "----------------------------" << std::endl;
w2e(0);
w2f(0);
w2g(0);
w2h(0);
std::cout << "----------------------------" << std::endl;
wrapper2<functor>{functor{}}(0);
wrapper2<functor>{static_cast<functor const &&>(functor{})}(0);
wrapper2<functor>{fc}(0);
wrapper2<functor>{fd}(0);
std::cout << "----------------------------" << std::endl;
(wrapper2<functor> const){functor{}}(0);
(wrapper2<functor> const){static_cast<functor const &&>(functor{})}(0);
(wrapper2<functor> const){fc}(0);
(wrapper2<functor> const){fd}(0);
}