[英]Is `boost::function<int(const char*)> f` a pointer or an object?
As per the article( https://theboostcpplibraries.com/boost.function ), which says that:根据文章( https://theboostcpplibraries.com/boost.function ),其中说:
boost::function
makes it possible to define a pointer to a function with a specific signature.boost::function
可以定义指向具有特定签名的 function 的指针。boost::function<int(const char*)> f
defines a pointer f that can point to functions that expect a parameter of type const char* and return a value of type int.boost::function<int(const char*)> f
定义了一个指针 f,它可以指向需要 const char* 类型参数并返回 int 类型值的函数。
How to comprehend that boost::function<int(const char*)> f
defines a pointer f that can point to a function.如何理解
boost::function<int(const char*)> f
定义了一个可以指向 function 的指针 f。
Doesn't f
is an object with the type of boost::function<int(const char*)>
, it's not a pointer indeed. f
不是类型为boost::function<int(const char*)>
的 object 吗,它确实不是指针。
@sehe Sorry, I forgot.
@sehe对不起,我忘了。 The link is added to the post.
该链接已添加到帖子中。 theboostcpplibraries.com/boost.function – John 37 mins ago
theboostcpplibraries.com/boost.function – 约翰 37 分钟前
Thanks for the added link.感谢您添加的链接。
Though I have his book in print and value his contributions to Boost introductory materials, I stand by my assessment that the wording here is unnecessarily confusing, and even misguiding: it seems to completely miss the fact that non-function callables can be assigned.尽管我已经出版了他的书并重视他对 Boost 介绍性材料的贡献,但我坚持我的评估,这里的措辞不必要地令人困惑,甚至是误导性的:它似乎完全忽略了可以分配非函数可调用对象的事实。 This, IMO, is the whole point of doing the type erasure.
IMO,这就是进行类型擦除的重点。
If you just wanted to define function pointers easily, you could just use function pointers:如果您只想轻松定义 function 指针,则可以使用 function 指针:
static int my_function(const char*) { return 42; }
// regular function pointer stuff
{
using MyFunction = int(const char*);
MyFunction* pf = &my_function;
if (pf) {
std::cout << "pf(): " << pf(argument) << "\n";
}
}
But with boost::function
(or std::function
) you can also do:但是使用
boost::function
(或std::function
)你也可以这样做:
// gereralized assignable callables:
boost::function<int(const char*)> f = &my_function;
if (f) {
std::cout << "same with f(): " << f(argument) << "\n";
}
struct MyCallable {
int operator()(const char*) const { return _retval; }
int _retval = 42;
};
f = MyCallable{};
std::cout << f(argument) << "\n"; // prints 42
f = MyCallable{99};
std::cout << f(argument) << "\n"; // prints 99
MyCallable instance { 10 };
f = std::ref(instance);
while (instance._retval--)
std::cout << f(argument) << "\n"; // prints 9 .. 0
#include <boost/function.hpp>
#include <iostream>
static int my_function(const char*) { return 42; }
int main() {
auto argument = "dummy";
// regular function pointer stuff
{
using MyFunction = int(const char*);
MyFunction* pf = &my_function;
if (pf) {
std::cout << "pf(): " << pf(argument) << "\n";
}
}
// gereralized assignable callables:
boost::function<int(const char*)> f = &my_function;
if (f) {
std::cout << "same with f(): " << f(argument) << "\n";
}
struct MyCallable {
int operator()(const char*) const { return _retval; }
int _retval = 42;
};
f = MyCallable{};
std::cout << f(argument) << "\n"; // prints 42
f = MyCallable{99};
std::cout << f(argument) << "\n"; // prints 99
MyCallable instance { 10 };
f = std::ref(instance);
while (instance._retval--)
std::cout << f(argument) << "\n"; // prints 9 .. 0
}
Prints印刷
pf(): 42
same with f(): 42
42
99
9
8
7
6
5
4
3
2
1
0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.