가변성 템플릿 덕분에 파이썬의 체인 함수와 비슷한 C++을 구현했습니다. 이 함수는 여러 컨테이너를 연속적으로 반복하는 데 사용됩니다.variadic 템플릿 템플릿을 사용한 타입 공제와 인수 전달
template<typename... Iterables>
auto chain(Iterables&&... iters)
-> ChainObject<Iterables...>
{
return /* ... */;
}
그리고 해당 주 :
int main()
{
std::vector<int> vec = { 1, 2, 3, 4, 5 };
std::list<int> li = { 6, 7, 8, 9, 10, 11, 12, 13 };
for (auto& i: chain(vec, li))
{
// You can edit a range of iterables
// as if there was only one of them.
i *= 5;
std::cout << i << std::endl;
}
return 0;
}
주요 잘 작동하는지 여기가 어떤 ChainedObject
라는 생성기를 사용하여 함수의 이전 작업 버전입니다. 우리는 ChainObject에있는 문제가 무엇인지 상관하지 않습니다. 그래서 봅시다. 내가 사용하는 다른 컬렉션이 같은 value_type
을 한 것으로 확인 템플릿 템플릿을 사용하려고 다음과 같은 방법으로 기능 chain
수정 :
template<typename T, template<typename...> class... Iterables>
auto chain(Iterables<T>&&... iters)
-> ChainObject<T, Iterables...>
{
return /* ... */;
}
나는 이것이 내 이전 주에서 list
및 vector
을 보장하기 위해 트릭을 할 것이라고 생각 동일한 유형을 공유하지만 대신에, 나는 GCC 4.7.1에서 다음과 같은 오류 얻을 :
In function 'int main()':
error: no matching function for call to 'chain(std::vector&, std::list&)'
note: candidates are:
note:
ChainObject<T, Iterables ...> chain(Iterables<T>&& ...) [with T = int; Iterables = {std::vector, std::list}
]note: no known conversion for argument 2 from '
std::list<int>
' to 'std::list<int>&&
'note:
ChainObject<T, Iterables ...> chain(Iterables<T>&& ...) [with T = int; Iterables = {std::vector, std::list}]
note: no known conversion for argument 2 from '
std::list<int>
' to 'std::list<int>&&
'error: unable to deduce 'auto&' from ''
문제를 rvalue 참조를 복용 함수에 전달 인자에서 오는 것으로 보인다합니다. 그러나, 나는 왜 나의 첫번째 버전이 잘 작동하는지 이해하지 못하고, 템플릿 템플릿을 사용하는 것에 주목한다.
는 당신이를 rvalue 대신 좌변 참조를 전달하는 시도? –
템플릿 템플릿을 컨테이너와 함께 사용하지 마십시오. 할당자가 사용되는 즉시 실패 할 것입니다. 그냥 중첩 된'value_type'의 동등성을 검사하십시오. – pmr