Trailing Return Type Using Dectype with a Variadic Template Function
When attempting to create a variational template function that sums arguments of varying types and returns an appropriately typed sum, common issues arise.
Problem Formulation
A basic implementation using decltype as the trailing return type results in undefined behavior for arguments exceeding two. To avoid this, the function can be declared explicitly, but this leads to incorrect type deduction for multiple arguments.
Solution Using Custom Traits Class
To overcome these issues, a custom traits class called sum_type is utilized. It recursively calculates the return type using std::add_rvalue_reference and std::val.
template<class T, class... P> struct sum_type; template<class T> struct sum_type<T> : id<T> {}; template<class T, class U, class... P> struct sum_type<T,U,P...> : sum_type< decltype( val<const T&>() + val<const U&>() ), P... > {};
Modified Implementation
By replacing decltype with typename sum_type
template <class T, class... P> auto sum(const T& t, const P&... p) -> typename sum_type<T,P...>::type { return t + sum(p...); }
Improved Type Deduction
Additionally, a modification to the last specialization of sum_type provides improved type deduction:
template<class T, class U, class... P> struct sum_type<T,U,P...> : id<decltype( val<T>() + val<typename sum_type<U,P...>::type>() )>{};
This ensures that the return type matches decltype(a+(b+c)), aligning with the expected addition order.
위 내용은 다양한 유형의 인수를 합하는 함수에 대한 올바른 반환 유형을 추론하기 위해 후행 반환 유형을 가변 템플릿 함수와 함께 어떻게 사용할 수 있습니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!