使用超载方法使用std :: result_of
Using std::result_of with an overloaded method
我在现有类中添加了一个超载方法,该类别现在在我们的单元测试中导致汇编错误。
我已经使用以下代码复制了问题:
#include <type_traits>
#include <string>
class Foo
{
public:
Foo() {};
int bar(const std::string & s) {return 1;};
int bar(const std::string & s, long l) {return 2;};
int bar2(const std::string & s) {return 3;};
};
int main()
{
// compiles
std::is_same<std::result_of<decltype(&Foo::bar2)(Foo, const std::string &)>::type, int>::value;
// does not compile
std::is_same<std::result_of<decltype(&Foo::bar)(Foo, const std::string &)>::type, int>::value;
return 0;
}
我需要对不编译的行进行哪些更改,以便我可以测试超载方法的返回?
一个超载函数名称代表所有过载,每个名称都有自己的地址。因此,除非存在支持上下文,例如,使用静态铸件:
static_cast<int(Foo::*)(const std::string&)>(&Foo::bar)
但是,这要求您知道确切的签名,这与查找返回类型的目标相矛盾。相反,您可以使用decltype
指示符和助手declval
函数查询返回类型:
std::is_same<decltype(std::declval<Foo&>().bar("")), int>::value
相关文章:
- Usages of std::move
- 使用 [] 运算符时"binding reference of type discards qualifiers"
- 如何处理 c++ 中类实现中的"invalid use of non-static data member"?
- 具有变量Number of Arguments的std::函数的矢量
- Capacity of a deque
- Deprecation of _writeBarrier()
- constexpr begin of a std::array
- 如何调整 std::vector of Eigen::MatrixXd 的大小
- C++ "error: invalid use of void expression"
- 我看到"use of undeclared identifier"错误,有人可以告诉我如何解决它吗?
- 赛通"Cannot take address of memoryview slice"
- C++ Version Of Double.longBitsToDouble
- 收到错误"invalid use of non-static data member 'stu::n' "
- C++ Usage of AsyncCallback
- 错误:"Left of getValue must have class/struct/union"
- Centos7 g++ "to_string is not in a member of std"
- 在C++中使用 Catch 测试框架编译错误"error: expected ';' at end of declaration list"
- 传递 std::vector of std::shared_ptr,而不是更新对象
- C++/SDL "initial value of reference to a non-const must be an lvalue"
- 摆脱编译器警告"warning: result of call is not used"