![](/img/trans.png)
[英]Why I can't use the std::map[ ] to add a string, but std::map.at() works?
[英]Why can't I use the gtest ValuesIn generator with a std::map?
我正在嘗試通過以下方式使用gtest在地圖的元素上運行一些測試:
typedef std::map< std::string, std::vector< std::string > > maptype;
static maptype generateMapOfElements() {...}
class MyTest : public TestWithParam< maptype::value_type > {
...
}
TEST_P(MyTest , my_first_test) { .. }
INSTANTIATE_TEST_CASE_P(test_prefix,
MyTest,
ValuesIn( generateMapOfElements() ) ); // <- problem
問題似乎是實例化pair::swap
會產生問題,因為地圖的鍵是const字符串(下面的完整日志)
1>C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\utility(48): error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const std::string' (or there is no acceptable conversion)
1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\xstring(912): could be 'std::basic_string<_Elem,_Traits,_Alloc> &std::basic_string<_Elem,_Traits,_Alloc>::operator =(std::basic_string<_Elem,_Traits,_Alloc> &&) throw()'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Alloc=std::allocator<char>
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\xstring(969): or 'std::basic_string<_Elem,_Traits,_Alloc> &std::basic_string<_Elem,_Traits,_Alloc>::operator =(const std::basic_string<_Elem,_Traits,_Alloc> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Alloc=std::allocator<char>
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\xstring(987): or 'std::basic_string<_Elem,_Traits,_Alloc> &std::basic_string<_Elem,_Traits,_Alloc>::operator =(const _Elem *)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Alloc=std::allocator<char>
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\xstring(992): or 'std::basic_string<_Elem,_Traits,_Alloc> &std::basic_string<_Elem,_Traits,_Alloc>::operator =(_Elem)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Alloc=std::allocator<char>
1> ]
1> while trying to match the argument list '(const std::string, const std::basic_string<_Elem,_Traits,_Alloc>)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Alloc=std::allocator<char>
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\utility(56) : see reference to function template instantiation 'void std::swap<_Ty>(_Ty &,_Ty &)' being compiled
1> with
1> [
1> _Ty=std::string
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\utility(107) : see reference to function template instantiation 'void std::_Swap_adl<_Ty1>(_Ty &,_Ty &)' being compiled
1> with
1> [
1> _Ty1=std::string,
1> _Ty=std::string
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\utility(104) : while compiling class template member function 'void std::pair<_Ty1,_Ty2>::swap(std::pair<_Ty1,_Ty2> &)'
1> with
1> [
1> _Ty1=const std::string,
1> _Ty2=std::vector<testing::internal::string>
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\utility(211) : see reference to function template instantiation 'void std::pair<_Ty1,_Ty2>::swap(std::pair<_Ty1,_Ty2> &)' being compiled
1> with
1> [
1> _Ty1=const std::string,
1> _Ty2=std::vector<testing::internal::string>
1> ]
1> C:\dir\my_test.cpp(251) : see reference to class template instantiation 'std::pair<_Ty1,_Ty2>' being compiled
1> with
1> [
1> _Ty1=const std::string,
1> _Ty2=std::vector<testing::internal::string>
1> ]
ValuesIn
函數返回一個生成器,該生成器保留值的副本,並且該副本存儲在vector
。 (為什么要存儲副本?因為通常來說,它不能依賴原始值源在測試的生命周期內保持有效。) vector
的要求之一是vector
的元素類型必須是可分配的(C + +03,第23.1 / 3節)。 但是,您已經注意到, map::value_type
是pair<const Key, T>
。 const鍵類型可防止分配該類型。
您可以考慮返回vector<pair<string, vector<string>>>
而不是生成器函數中的map<string, vector<string>>
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.