동일한 유형의 두 튜플 사이에서 산술 연산을 수행하기 위해 +
과 같은 템플릿 함수/연산자를 작성하려고합니다. 산술 요소 많다는을 할 예를 들어,C++에서 튜플 산술을 수행하는 방법 (C++ 11/C++ 17)?
std::tuple<int,double> t = std::make_tuple(1,2);
위해 나는 논리는 간단하다
auto t1 = t + t;
할 싶습니다. 그러나 C++ 템플릿 프로그래밍 (C++ 11/17)에서이 작업을 수행하는 방법을 알아낼 수는 없습니다. 아래 코드는 g++ -std=c++11 tuple_arith.cpp
으로 컴파일되지 않습니다. 특히, 일반 add
함수 (template<typename T> T add(T x, T y) { return x + y; }
)를 터플 조작 코드와 함께 사용하는 올바른 방법을 이해할 수 없습니다.
누군가 문제 해결 방법을 설명해 줄 수 있습니까?
#include <tuple>
namespace std {
template<typename _Tp, size_t __i, size_t __size, typename _opT >
struct __tuple_arith {
static constexpr _Tp __op(const _Tp& __t, const _Tp& __u, const _opT& op) {
return std::tuple_cat(std::make_tuple(op(std::get<__i>(__t), std::get<__i>(__u))
, __tuple_arith<_Tp, __i + 1, __size, _opT>::__op(__t, __u)));
}
};
template<typename _Tp, size_t __size, typename _opT>
struct __tuple_arith<_Tp, __size, __size - 1, _opT> {
static constexpr _Tp __op(const _Tp& __t, const _Tp& __u, const _opT& op) {
return std::make_tuple(op(std::get<__size-1>(__t), std::get<__size -1>(__u)));
}
};
template<typename T> T add(T x, T y) { return x + y; }
template<typename... _TElements> constexpr tuple<_TElements...>
operator+(const tuple<_TElements...>& __t, const tuple<_TElements...>& __u) {
using op = __tuple_arith<tuple<_TElements...>, 0, sizeof...(_TElements), decltype(add)>;
return op::__op(__t, __u, add);
}
}; //namespace std
#include <iostream>
using namespace std;
int main() {
std::tuple<int,double> t = std::make_tuple(1,2);
auto t1 = t + t;
cout << std::get<0>(t1) << std::endl;
return 0;
}
특정 오류는 다음과 같습니다
tuple_arith.cpp:14:10: error: template argument ‘(__size - 1)’ involves template parameter(s)
struct __tuple_arith<_Tp, __size, __size - 1, _opT> {
^
tuple_arith.cpp: In function ‘constexpr std::tuple<_Elements ...> std::operator+(const std::tuple<_Elements ...>&, const std::tuple<_Elements ...>&)’:
tuple_arith.cpp:24:90: error: decltype cannot resolve address of overloaded function
__tuple_arith<tuple<_TElements...>, 0, sizeof...(_TElements), decltype(add)>;
^
tuple_arith.cpp:24:91: error: template argument 4 is invalid
__tuple_arith<tuple<_TElements...>, 0, sizeof...(_TElements), decltype(add)>;
^
tuple_arith.cpp:25:12: error: ‘op’ has not been declared
return op::__op(__t, __u, add);
^
tuple_arith.cpp: In instantiation of ‘constexpr std::tuple<_Elements ...> std::operator+(const std::tuple<_Elements ...>&, const std::tuple<_Elements ...>&) [with _TElements = {int, double}]’:
tuple_arith.cpp:34:17: required from here
tuple_arith.cpp:26:3: error: body of constexpr function ‘constexpr std::tuple<_Elements ...> std::operator+(const std::tuple<_Elements ...>&, const std::tuple<_Elements ...>&) [with _TElements = {int, double}]’ not a return-statement
}
^
- 업데이트 - 지금까지 도움이 답변을
감사합니다. 운영자 래퍼 (Operator Wrappers)에서 작동하도록 할 수 있습니까? std::{plus,minus,multiplies,divides}
? 그게 내가 템플릿 매개 변수 typename _opT
으로 달성하려고했던 것입니다. 결국, 호환 연산자를 매개 변수로 취할 수있는 함수/개체를 찾고 있습니다.
이 네임 스페이스 std''에 자신의 기능을 주입하는 것은 불법입니다. '__' 또는'_'로 시작하는 식별자 뒤에 대문자가 오도록하는 것은 불법입니다. 이것들이 코드가 작동하지 않는 이유가 아니기 때문에, 대부분의 컴파일러가 당신에게 전화하지 않을 것이지만, 그렇게하지 말아야합니다. – Yakk
그냥 https://ideone.com/giSbxM에 무엇이 문제가 있습니까? – gurka
@gurka Ostensibly OP는 두 개보다 많거나 적은 요소가있는 '튜플'로 우아하게 확장되는 솔루션을 찾고 있습니다. – Xirema