gpt4 book ai didi

c++ - 为什么这行得通?

转载 作者:搜寻专家 更新时间:2023-10-31 00:22:38 24 4
gpt4 key购买 nike

我刚刚使用 boost::bindboost::function 并注意到以下行为(我认为这有点奇怪)。您可以绑定(bind)一个参数少于 boost::function 类型所需参数的函数!似乎任何附加参数都被简单地忽略了,就这样消失了。

那么为什么这种行为是正确的呢?我的期望是应该引发一个编译错误,说明不兼容。

请参阅下面显示问题的工作代码示例

#include "boost/bind.hpp"
#include "boost/function.hpp"

namespace
{
int binder(const char& testChar,
const int& testInt,
const std::string& testString)
{
return 3;
}

}

int main(int c, char** argv)
{
boost::function<int(const char&,
const int&,
const std::string&,
const float&,
const std::string&,
const int&)> test;
test = boost::bind(&::binder, _1, _2, _3);

std::cout << test('c', 1, "something", 1.f, "more", 10) << std::endl;

}

最佳答案

这不是 boost::bind 的目的 - 允许您重新映射函数的原型(prototype)吗?您正在使 test 可用于 6 个输入参数,而您的基础函数只需要 3 个。

本页:http://blog.think-async.com/2010/04/bind-illustrated.htmlboost::bind 的工作原理有很好的概述。

关于c++ - 为什么这行得通?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3082670/

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