在完美转发中,std::forward用于转换命名的右值引用t1和t2未命名的右值引用.这样做的目的是什么?inner如果我们离开t1&t2作为左值,那将如何影响被调用的函数?
template <typename T1, typename T2>
void outer(T1&& t1, T2&& t2)
{
inner(std::forward<T1>(t1), std::forward<T2>(t2));
}
Run Code Online (Sandbox Code Playgroud) #include <functional>
int foo(void) {return 2;}
class bar {
public:
int operator() (void) {return 3;};
int something(int a) {return a;};
};
template <class C> auto func(C&& c) -> decltype(c()) { return c(); }
template <class C> int doit(C&& c) { return c();}
template <class C> void func_wrapper(C&& c) { func( std::bind(doit<C>, std::forward<C>(c)) ); }
int main(int argc, char* argv[])
{
// call with a function pointer
func(foo);
func_wrapper(foo); // error
// call with a member …Run Code Online (Sandbox Code Playgroud) 我想知道如何做到以下几点
void f(string &&s) {
std::string i(move(s));
/* other stuff */
}
int main() {
std::string s;
bind(f, s)(); // Error.
bind(f, move(s))(); // Error.
bind(f, ref(s))(); // Error.
}
Run Code Online (Sandbox Code Playgroud)
如何传递rvalue引用并将其作为rvalue引用(可能包装)存储在调用包装器中?我知道我可以手动编写一个类似于std::reference_wrapper<>具有转换功能的类T&&,但我宁愿避免这种情况并使用标准技术.
我像AProgrammer推荐的那样实现了它:
template<typename T> struct adv {
T t;
explicit adv(T &&t):t(forward<T>(t)) {}
template<typename ...U> T &&operator()(U &&...) {
return forward<T>(t);
}
};
template<typename T> adv<T> make_adv(T &&t) {
return adv<T>{forward<T>(t)};
}
namespace std {
template<typename T>
struct is_bind_expression< adv<T> > : std::true_type {};
}
Run Code Online (Sandbox Code Playgroud)
现在我可以说 …
我正在尝试使以下代码工作:
#include <cstdio>
#include <functional>
#include <string>
#include <memory>
using namespace std;
class Foo {
public:
Foo(): m_str("foo") { }
void f1(string s1, string s2, unique_ptr<Foo> p)
{
printf("1: %s %s %s\n", s1.c_str(), s2.c_str(), p->str());
}
void f2(string s1, string s2, Foo* p)
{
printf("2: %s %s %s\n", s1.c_str(), s2.c_str(), p->str());
}
const char* str() const { return m_str.c_str(); }
private:
string m_str;
};
int main()
{
string arg1 = "arg1";
string arg2 = "arg2";
Foo s;
unique_ptr<Foo> ptr(new Foo); …Run Code Online (Sandbox Code Playgroud) #include <functional>
#include <iostream>
#include <string>
#include <utility>
using namespace std;
template<typename Func, typename... Args>
void do_task(Func &func, Args &&...args)
{
auto f = bind(func, forward<Args>(args)...);
f();
}
int main()
{
string name = "hello";
auto test_func = [](string name, string &&arg1, string &&arg2) -> void {
cout << name << " " << arg1 << " " << arg2 << endl;
};
// do_task(test_func,"test_one", "hello", string("world")); // compile error
// do_task(test_func, "test_tww", std::move(name), "world"); // compile error
do_task(test_func, "test_three", …Run Code Online (Sandbox Code Playgroud) C++20 增加了可寻址函数 规则16.5.4.2.1 [namespace.std]/6 : -- 重点是我的 --
让F表示标准库函数([global.functions])、标准库静态成员函数或标准库函数模板的实例化。除非F被指定为 可寻址函数,否则C++ 程序的行为是未指定的(可能格式错误),如果它显式或隐式地尝试形成指向 F 的指针。 [注意:形成此类指针的可能方法包括应用一元 &运算符 ([expr.unary.op])、addressof ([specialized.addressof]) 或函数到指针的标准转换 ([conv.func])。— 尾注 ] 此外,如果 C++ 程序试图形成对 F 的引用,或者如果它试图形成一个指向成员的指针来指定标准库非静态成员函数([member .functions]) 或标准库成员函数模板的实例化。
据我所知,数学函数没有被规范标记为可寻址函数。
这是否意味着以下代码自 C++20 以来是非法的(如cppreference和其他标准库函数的示例所指出的):
// unspecified and illegal?
auto func = static_cast<float (*)(float, float)>(std::pow);
std::cout << func(2, 4) << std::endl;
Run Code Online (Sandbox Code Playgroud)
下面的代码呢,它合法吗?
// legal? or unspecified and illegal?
std::function<float(float, float)> f = static_cast<float(*)(float, float)>(std::pow);
std::cout …Run Code Online (Sandbox Code Playgroud) 我有一些试图锁定的函数,std::mutex如果mutex成功锁定,函数std::thread使用lambda函数作为线程函数参数创建,并使用std::move()以下命令传递它:
static std::mutex mtx;
// some mutex defended stuff
void someFunc() {
// try lock mutex using unique_lock
std::unique_lock<std::mutex> lock(mtx, std::try_to_lock);
if(!lock.owns_lock()) {
return; // locking fails
}
// task for thread
auto task = [&](std::unique_lock<std::mutex>&& lock) {
// do async work and release lock when it done
lock.unlock();
// do something after releasing lock
};
// create thread, pass lock
std::thread taskThread(task, std::move(lock));
taskThread.detach();
}
Run Code Online (Sandbox Code Playgroud)
我有编译器错误:
<lambda_1918cc58d906c210588b1a8bb33f1b0d>::operator
()(std::unique_lock<_Mutex> &&) const' : cannot convert …Run Code Online (Sandbox Code Playgroud)