공제 가이드가 범용 참조 및 std::forward
과 함께 작동하는 방법, 특히 완벽하게 전달하는 래퍼를 만드는 방법을 알고 싶습니다. 아래의 코드는 펑터 래퍼 (implator)가있는 경우와 명시 적 공제 가이드가있는 경우의 두 가지 경우에서 펑터 래퍼 (functor wrapper)를 실험 할 수있는 코드를 제공합니다.클래스 템플릿 인수 공제와 함께 완벽한 전달
완벽한 전달을 위해 어디에 필요한지 알 수 없기 때문에 나는 &&
과 std::forward
을 많이 썼습니다. 나는 그 (것)들을 어디에 두어야하는지, 그리고 그것이 필요하지 않은 곳을 알고 싶다.
// Case with not conversion constructor
template <class F>
struct functor1
{
explicit constexpr functor1(F/*&&*/ f)
noexcept(std::is_nothrow_copy_constructible_v<F/*&&*/>)
: _f(/*std::forward<F>(*/f/*)*/)
{}
template <class... Args>
constexpr operator()(Args&&... args)
noexcept(std::is_nothrow_invocable_v<F/*&&*/, Args/*&&*/...>)
{
/*std::forward<F>(*/_f/*)*/(std::forward<Args>(args)...);
}
private: F/*&&*/ _f;
};
// Case with a conversion constructor
template <class F>
struct functor2
{
template <class G>
explicit constexpr functor2(G&& g)
noexcept(std::is_nothrow_constructible_v<G/*&&*/, F/*&&*/>)
: _f(/*std::forward<G>(*/g/*)*/)
{}
template <class... Args>
constexpr operator()(Args&&... args)
noexcept(std::is_nothrow_invocable_v<F/*&&*/, Args/*&&*/...>)
{
/*std::forward<F>(*/_f/*)*/(std::forward<Args>(args)...);
}
private: F/*&&*/ _f;
};
template <class G>
functor2(G&&) -> functor2<G/*&&*/>;
편집 : 단순의 술과는 앞의 예에서, 질문의 요점이 아니기 때문에, 우리는 F
및 G
기능이 operator()
으로 즉, 클래스/구조체를 객체 것을 고려하십시오.