`boost::function<int(const char*)> f` 是指针还是对象?
Is `boost::function<int(const char*)> f` a pointer or an object?
根据文章(https://theboostcpplibraries.com/boost.function),其中说:
boost::function
makes it possible to define a pointer to a function
with a specific signature. 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
难道f
不是boost::function<int(const char*)>
类型的对象吗,确实不是指针
@sehe Sorry, I forgot. The link is added to the post. theboostcpplibraries.com/boost.function – John 37 mins ago
感谢您添加 link。
虽然我已经出版了他的书并且重视他对 Boost 介绍材料的贡献,但我坚持我的判断,这里的措辞不必要地令人困惑,甚至误导:它似乎完全忽略了非函数可调用对象这一事实可以分配。在我看来,这就是进行类型擦除的全部意义所在。
如果你只是想方便地定义函数指针,你可以直接使用函数指针:
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";
}
}
但是使用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
}
版画
pf(): 42
same with f(): 42
42
99
9
8
7
6
5
4
3
2
1
0
根据文章(https://theboostcpplibraries.com/boost.function),其中说:
boost::function
makes it possible to define a pointer to a function with a specific signature.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
难道f
不是boost::function<int(const char*)>
类型的对象吗,确实不是指针
@sehe Sorry, I forgot. The link is added to the post. theboostcpplibraries.com/boost.function – John 37 mins ago
感谢您添加 link。
虽然我已经出版了他的书并且重视他对 Boost 介绍材料的贡献,但我坚持我的判断,这里的措辞不必要地令人困惑,甚至误导:它似乎完全忽略了非函数可调用对象这一事实可以分配。在我看来,这就是进行类型擦除的全部意义所在。
如果你只是想方便地定义函数指针,你可以直接使用函数指针:
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";
}
}
但是使用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
}
版画
pf(): 42
same with f(): 42
42
99
9
8
7
6
5
4
3
2
1
0