为什么我不能使用带有std :: map的测试值In In生成器?

时间:2014-04-30 15:44:12

标签: c++ templates googletest

我正试图通过以下方式对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>          ]

1 个答案:

答案 0 :(得分:3)

ValuesIn函数返回一个保留值的副本的生成器,该副本存储在vector中。 (为什么它存储副本?因为通常,它不能依赖原始值源在测试的生命周期内保持有效。)vector的一个要求是它的元素类型必须是可分配的(C ++ 03,§23.1/ 3)。但是,正如您已经注意到的那样,map::value_typepair<const Key, T>。 const键类型阻止类型可分配。

您可以考虑返回map<string, vector<string>>,而不是生成器函数中的vector<pair<string, vector<string>>>