Trompeloeil MAKE_MOCK0 模板为 return 类型
Trompeloeil MAKE_MOCK0 with a template as a return type
在 C++ 中使用 Trompeloeil 模拟单元测试时,如何将 unordered_map
用作 return 类型?
// This example works fine
// return type is void, my_function takes no arguments
MAKE_MOCK0(my_function, void(), override);
// This is what I'm trying to do but fails
MAKE_MOCK0(my_function, std::unordered_map<std::string, int>(), override);
Visual Studio 出现以下 IntelliSense 错误,
- C2976 -
std::unordered_map
: 模板参数太少
- C2955 -
std::unordered_map
: class 模板的使用需要模板参数列表
- C2923 -
trompeloeil::param_list
: std::_Hash<_Traits::size>
不是参数类型 T
的有效模板类型参数
- C2143 - 在
;
之前缺少 >
的语法错误
- C2955 -
trompeloeil::param_list
使用 class 模板需要模板参数列表
- C2338 - 函数签名没有 0 个参数
- C3203 -
unordered_map
未专门化 class 不能用作模板参数 'Sig' 的模板参数,需要一个真实类型
- C4346 -
std::unordered_map::type
依赖名称不是类型
- C2923 -
trompeloeil::identity_type
: std::unordered_map::type
不是参数类型 T
的有效模板类型参数
- C3203 -
unordered_map
未专门化 class 不能用作模板参数 'T' 的模板参数,需要一个真实类型
模板化 return 类型需要包装在 (...)
中。解释如下:Q. Why can't I mock a function that returns a template?
// Bad
MAKE_MOCK0(my_function, std::unordered_map<std::string, int>(), override);
// Good
MAKE_MOCK0(my_function, (std::unordered_map<std::string, int>()), override);
C++ 预处理器在解析具有多个参数的模板时可能会出现问题 std::string, int
。如果发生这种情况,将 return 类型移动到 typedef 会有所帮助。
typedef std::unordered_map<std::string, int> My_Type;
...
MAKE_MOCK0(my_function, (My_Type()), override);
在 C++ 中使用 Trompeloeil 模拟单元测试时,如何将 unordered_map
用作 return 类型?
// This example works fine
// return type is void, my_function takes no arguments
MAKE_MOCK0(my_function, void(), override);
// This is what I'm trying to do but fails
MAKE_MOCK0(my_function, std::unordered_map<std::string, int>(), override);
Visual Studio 出现以下 IntelliSense 错误,
- C2976 -
std::unordered_map
: 模板参数太少 - C2955 -
std::unordered_map
: class 模板的使用需要模板参数列表 - C2923 -
trompeloeil::param_list
:std::_Hash<_Traits::size>
不是参数类型T
的有效模板类型参数
- C2143 - 在
;
之前缺少 - C2955 -
trompeloeil::param_list
使用 class 模板需要模板参数列表 - C2338 - 函数签名没有 0 个参数
- C3203 -
unordered_map
未专门化 class 不能用作模板参数 'Sig' 的模板参数,需要一个真实类型 - C4346 -
std::unordered_map::type
依赖名称不是类型 - C2923 -
trompeloeil::identity_type
:std::unordered_map::type
不是参数类型T
的有效模板类型参数
- C3203 -
unordered_map
未专门化 class 不能用作模板参数 'T' 的模板参数,需要一个真实类型
>
的语法错误
模板化 return 类型需要包装在 (...)
中。解释如下:Q. Why can't I mock a function that returns a template?
// Bad
MAKE_MOCK0(my_function, std::unordered_map<std::string, int>(), override);
// Good
MAKE_MOCK0(my_function, (std::unordered_map<std::string, int>()), override);
C++ 预处理器在解析具有多个参数的模板时可能会出现问题 std::string, int
。如果发生这种情况,将 return 类型移动到 typedef 会有所帮助。
typedef std::unordered_map<std::string, int> My_Type;
...
MAKE_MOCK0(my_function, (My_Type()), override);