unique_ptr
형식이 C++ 11에서 템플릿과 얼마나 잘 작동하는지 파악하려고하는데 많은 행운이 없습니다. 특히 템플릿 함수를 삽입하려고합니다. a unique_ptr
지도에서 키의 값이 아직 없으면 주어진 맵에서 주어진 고유 키 값이고 unique_ptr
이 가리키는 값의 소유권을 맵의 값으로 이동합니다. 키가 이미 있으면 런타임 오류가 발생합니다. 당신이 함수에 전달하지 않는 경우템플릿 함수에 대한 고유 한 포인터 전달
이제, unique_ptr
의 값을 이동하는 것은 매우 간단하다 :
#include <iostream>
#include <memory>
#include <unordered_map>
using namespace std;
int main(int argc, char *argv[])
{
unordered_map<char, unique_ptr<int> > testMap;
char key1 = 'A';
unique_ptr<int> val1(new int(1));
testMap[key1] = move(val1);
// Print the results
cout << "testMap[" << key1 << "] = " << *testMap[key1] << endl;
return 0;
}
비 템플릿 함수
함수에 unique_ptr
을 전달하다 더 복잡한 비트 :
#include <iostream>
#include <memory>
#include <unordered_map>
using namespace std;
void moveValueForUniqueKey(unordered_map<char, unique_ptr<int> >& targetMap, char key, unique_ptr<int> value) throw(char)
{
// Check if the key is already in the map
auto it = targetMap.find(key);
if (it != targetMap.end())
{
throw key;
}
else
{
targetMap[key] = move(value);
}
}
int main(int argc, char *argv[])
{
unordered_map<char, unique_ptr<int> > testMap;
char key1 = 'A';
unique_ptr<int> val1(new int(1));
// Try inserting the first key-value pair
try
{
moveValueForUniqueKey(testMap, key1, move(val1));
}
catch (char& duplicateKey)
{
cerr << "Key '" << duplicateKey << "' already in map." << endl;
}
// Print the key-value pairs
for (pair<const char, unique_ptr<int> >& entry : testMap)
{
cout << "testMap['" << entry.first << "'] = " << *entry.second << endl;
}
unique_ptr<int> val2(new int(2));
// Try inserting the key again
try
{
moveValueForUniqueKey(testMap, key1, move(val2));
}
catch (char& duplicateKey)
{
cerr << "Key '" << duplicateKey << "' already in map." << endl;
}
// Print the key-value pairs again
for (pair<const char, unique_ptr<int> >& entry : testMap)
{
cout << "testMap['" << entry.first << "'] = " << *entry.second << endl;
}
return 0;
}
이 코드 출력 :
,testMap['A'] = 1
Key 'A' already in map.
testMap['A'] = 1
기능 템플릿
이제, 내가 함께 moveValueForUniqueKey(...)
의 이전 선언/구현을 대체,이 함수의 템플릿을 만들려고 :
template<typename K, typename V, typename M>
void moveValueForUniqueKey(M targetMap, K key, unique_ptr<V> value) throw(char)
{
// Check if the key is already in the map
auto it = targetMap.find(key);
if (it != targetMap.end())
{
throw key;
}
else
{
targetMap[key] = move(value);
}
}
// Instantiate template function
template
void moveValueForUniqueKey(unordered_map<char, unique_ptr<int> >& targetMap, char key, unique_ptr<int> value) throw(char);
단순히 컴파일러 오류 use of deleted function ‘constexpr std::pair<_T1, _T2>::pair(const std::pair<_T1, _T2>&) [with _T1 = const char, _T2 = std::unique_ptr<int>, std::pair<_T1, _T2> = std::pair<const char, std::unique_ptr<int> >]’
를 얻을.
여기 정확히 무슨 일이 벌어지고 있으며, 내가 여기에서 시도하려는 것을 성취 할 수 있습니까? (이동 시맨틱을 사용하여 unique_ptr
개체를 템플릿 함수로 전달하십시오)? 모든
'remove_reference &> :: type'입니다 중복 ... –
0x499602D2
이해가 안 : 왜'remove_reference &> : type'이 아니라 'unique_ptr '? –
좋아, 그거 이상해. 그걸로 놀고 다니는 동안 나는 그것을 필요로한다고 생각했지만, 단지'unique_ptr'으로 컴파일하는 것 같다. ... –
errantlinguist