假设我有一个函数,采用右值引用:
void whatever (std::unique_ptr<int>&&) {
// Nothing!
}
Run Code Online (Sandbox Code Playgroud)
...并将其一个参数绑定到占位符.
auto f = std::bind(&whatever, _1);
Run Code Online (Sandbox Code Playgroud)
我尝试了这样的调用,结果与我的期望相反.
std::unique_ptr<int> nothing;
f(std::move(nothing)); // Fails to compile!
f(nothing); // Works, but seems wrong!
Run Code Online (Sandbox Code Playgroud)
这是编译器错误吗?或者,工作调用是不安全的代码?或者为什么我没有std::move这个指针进入绑定函数?
顺便说一句,gcc4.4的编译错误是:
test.cxx:14: error: no match for call to '(std::_Bind<void (*(std::_Placeholder<1>))(std::unique_ptr<int, std::default_delete<int> >&&)>) (std::unique_ptr<int, std::default_delete<int> >)'
Run Code Online (Sandbox Code Playgroud) 我正在尝试在我的 lambda 中使用 std::bind :
#include <functional>
#include <iostream>
#include <string>
struct Foo {
Foo() {}
void func(std::string input)
{
std::cout << input << '\n';
}
void bind()
{
std::cout << "bind attempt" << '\n';
auto f_sayHello = [this](std::string input) {std::bind(&Foo::func, this, input);};
f_sayHello("say hello");
}
};
int main()
{
Foo foo;
foo.bind();
}
Run Code Online (Sandbox Code Playgroud)
当我运行这段代码时,我所期望的是看到以下输出
bind attempt
say hello
Run Code Online (Sandbox Code Playgroud)
但我只看到“绑定尝试”。我很确定 lambda 有一些我不明白的地方。
让我展示一下我的意思.
#include <functional>
#include <iostream>
class MyClass
{
private:
int number;
public:
MyClass()
{
number = 0;
}
void printNumber()
{
std::cout << "number is " << number << std::endl;
number++;
}
};
int main()
{
std::shared_ptr<MyClass> obj = std::make_shared<MyClass>();
auto function = std::bind(&MyClass::printNumber, obj);
function();
// This deallocates the smart pointer.
obj.reset();
// This shouldn't work, since the object does not exist anymore.
function();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这输出:
number is 0
number is 1
Run Code Online (Sandbox Code Playgroud)
如果我们像通常那样调用函数,将"function()"替换为"obj-> printNumber()",则输出为:
number is 0 …Run Code Online (Sandbox Code Playgroud) 是否定义了以下代码的行为?f()通话如何表现?
#include <functional>
#include <iostream>
struct A
{
void shout()
{
std::cout <<"shout";
}
};
int main()
{
std::function<void()> f;
{
A a;
f = std::bind(&A::shout, &a);
}
f(); // what happens here?
}
Run Code Online (Sandbox Code Playgroud) 我需要将一个参数绑定到类成员函数。像这样的东西:
#include <functional>
#include <iostream>
struct test
{
void func(int a, int b)
{
std::cout << a << " " << b << std::endl;
}
};
int main(int argc, char** argv)
{
typedef void (test::*TFunc)(int);
TFunc func = std::bind(&test::func, 1, std::placeholders::_1);
}
Run Code Online (Sandbox Code Playgroud)
但在这种情况下我有编译错误
error: static assertion failed: Wrong number of arguments for pointer-to
-member
Run Code Online (Sandbox Code Playgroud) 我目前正在尝试编写一个程序,该程序使用std::binda std::filesystem::path和std::ostream,作为引用,如下所示:
#include <functional>
#include <filesystem>
#include <ostream>
#include <iostream>
struct Buggy{
static void something(const std::filesystem::path &path, std::ostream &out);
void bind();
};
void Buggy::bind(){
auto function = std::bind(&Buggy::something, std::placeholders::_1, std::cout);
function(std::filesystem::path("./"));
}
void Buggy::something(const std::filesystem::path &path, std::ostream &out){
out << path.string() << std::endl;
}
int main(){
Buggy buggy;
buggy.bind();
}
Run Code Online (Sandbox Code Playgroud)
我希望这段代码只输出" ./",但相反,它给了我大量的模板错误.为什么是这样?我对std::bind外观的使用对我来说是正确的.我g++ --std=c++17 bug4.cpp -o bug4 -lstdc++fs在Linux上编译.
我无法读取模板错误,因为它们与此标准库的实现细节混杂在一起.我尝试使用clang和gcc进行编译,两者都会产生类似的错误.通过搜索引擎搜索没有给出有用的结果.
我有以下的节目里我试图打印出乘法表10,使用的组合lambda functions和std::bind.主要目的是学习上述两个概念.然而,我得到的输出(如下所示)很奇怪.
#include <iostream>
#include <vector>
#include <algorithm>
#include <thread>
#include <zconf.h>
#include <functional>
using namespace std;
using namespace std::placeholders;
int multiply(int a, int b, int c)
{
return a*b*b;
}
int main()
{
auto f = std::bind(multiply, 5 , 2 , _1);
vector<int> vec = {1,2,3,4,5,6,7,8,9,10};
for_each(vec.begin(), vec.end(), [f](int &v) {cout << "Multiplication Table (10) :" << "v: " << v << " " << f(v) << endl;});
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
Multiplication Table (10) …Run Code Online (Sandbox Code Playgroud) 我想将c++ objcet 成员回调 func传递给一个只有 void*的“c” lib。然后我使用 std::function 来注册回调处理程序。但是当我将 std::function<>* 转换为 void* 时出现问题,似乎当转换为 void* 时 std::function<>* 被破坏了。
下面是测试代码。
#include <iostream>
#include <functional>
class TfLight {
public:
void print(int a) {
std::cout << "TfLight::print " << a << std::endl;
}
};
class Server {
public:
void run(int a) {
std::function<void(int)>* func = reinterpret_cast<std::function<void(int)>*>(ptr);
std::cout << func << std::endl;
(*func)(a);
}
void register(std::function<void(int)> tf) {
std::cout << (&tf) << std::endl;
// f = tf;
setPtr(&tf);
}
void setPtr(void* p) …Run Code Online (Sandbox Code Playgroud)