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 错误,

模板化 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);