gpt4 book ai didi

c++ - 使用 std::is_same、std::result_of 和 std::bind 时,static_assert 无法按预期工作

转载 作者:行者123 更新时间:2023-11-27 22:57:46 24 4
gpt4 key购买 nike

当我使用 static_assert 断言 function object 的返回类型与另一个类型相同时,我遇到了一个奇怪的行为。类似的东西(这段代码只是为了展示问题,而不是我真正想做的)。

int foo() {
return 0;
}

int main() {
auto funcObj = std::bind(foo);
static_assert(std::is_same<std::result_of<funcObj()>, int>::value, "FuncObj return type is not int");
return 0;
}

断言失败。这里出了什么问题?

最佳答案

std::result_of<?>是直接使用的无用类型。

typename std::result_of<?>::typestd::result_of_t<?> (在 C++14 中)你想要什么。

对于此类事情,获得​​正确错误消息的一种简单方法是:

std::result_of<decltype(funcObj)()> x = 3;

它应该生成错误消息,清楚地表明 lhs 类型不是 int像你预期的那样。 (通常无法将 int 转换为“一些复杂类型”)。

关于c++ - 使用 std::is_same、std::result_of 和 std::bind 时,static_assert 无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30917607/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com