在完美转发中,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) 我在这里看到了这一点: Move Constructor调用基类Move Constructor
有人能解释一下:
std::move和std::forward,优选用一些代码示例?可能重复:
有人可以向我解释移动语义吗?
我最近参加了一个C++ 11研讨会,并给出了以下一些建议.
when you have && and you are unsure, you will almost always use std::move
Run Code Online (Sandbox Code Playgroud)
任何人都可以向我解释为什么你应该使用std::move而不是某些替代品以及某些不应该使用的情况std::move?
寻找一种方法来实现一个通用的通用memoization函数,它将获取一个函数并返回相同的memoized版本?
在python中寻找像@memo(来自Norving的网站)装饰的东西.
def memo(f):
table = {}
def fmemo(*args):
if args not in table:
table[args] = f(*args)
return table[args]
fmemo.memo = table
return fmemo
Run Code Online (Sandbox Code Playgroud)
更一般的是,有没有办法在C++中表达泛型和可重用的装饰器,可能使用C++ 11的新功能?
我sprintf在C++ 11中使用函数,方法如下:
std::string toString()
{
std::string output;
uint32_t strSize=512;
do
{
output.reserve(strSize);
int ret = sprintf(output.c_str(), "Type=%u Version=%u ContentType=%u contentFormatVersion=%u magic=%04x Seg=%u",
INDEX_RECORD_TYPE_SERIALIZATION_HEADER,
FORAMT_VERSION,
contentType,
contentFormatVersion,
magic,
segmentId);
strSize *= 2;
} while (ret < 0);
return output;
}
Run Code Online (Sandbox Code Playgroud)
有没有比这更好的方法来检查每次保留的空间是否足够?为了将来添加更多东西的可能性.
我想创建类似通用工厂方法的东西 - 看看这个:
template <class BaseType>
class Factory {
public:
template <class ... Args>
static BaseType* Create(const Args& ... args) {
return new DerivedType(args ...);
}
};
Run Code Online (Sandbox Code Playgroud)
其中的DerivedType某些其他类型来自BaseType不同的地方并在其他地方定义.
问题在于存储DerivedType.我想这样做,例如,像这样:
void f() {
// Derived type may have more than one constructor,
// that's why I suggest using of the variadic templates.
BaseType* ptr1 = Factory<BaseType>::Create("abc", 5, 10.);
BaseType* ptr2 = Factory<BaseType>::Create();
...
}
...
Factory<BaseType>::SetType<MyDerivedType>();
f();
Factory<BaseType>::SetType<YourDerivedType>();
f();
Run Code Online (Sandbox Code Playgroud)
我可以设置不同的派生类型,但所有这些类型在编译时都是已知的.我想不出适当的技术来做到这一点.
问题:你能告诉一个吗?
这样做的理由(因此,原始问题,如果有人建议问题是它自己的XY问题) - 是一种单元测试一些棘手的代码部分的能力.例如,如果我有一个代码: …
我有一个名为PushC++ 11中编写的方法的以下模板类(剥离以仅包含相关部分):
template<class T, int Capacity>
class CircularStack
{
private:
std::array<std::unique_ptr<T>, Capacity> _stack;
public:
void Push(std::unique_ptr<T>&& value)
{
//some code omitted that updates an _index member variable
_stack[_index] = std::move(value);
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:
我应该使用
std::move还是std::forward在内Push?
我不确定是否std::unique_ptr<T>&&有资格作为通用参考,因此应该使用forward而不是move.
我是C++的新手.
我想实现一个小线程包装器,如果一个线程仍处于活动状态,或者该线程已完成其工作,它将提供信息.为此,我需要将函数及其参数传递给线程类到另一个函数.我有一个简单的实现应该可以工作,但不能让它编译,我无法弄清楚该怎么做才能使它工作.
这是我的代码:
#include <unistd.h>
#include <iomanip>
#include <iostream>
#include <thread>
#include <utility>
class ManagedThread
{
public:
template< class Function, class... Args> explicit ManagedThread( Function&& f, Args&&... args);
bool isActive() const { return mActive; }
private:
volatile bool mActive;
std::thread mThread;
};
template< class Function, class... Args>
void threadFunction( volatile bool& active_flag, Function&& f, Args&&... args)
{
active_flag = true;
f( args...);
active_flag = false;
}
template< class Function, class... Args>
ManagedThread::ManagedThread( Function&& f, Args&&... args):
mActive( false),
mThread( threadFunction< Function, …Run Code Online (Sandbox Code Playgroud) 在下面的代码中,为什么我应该std::forward在传递参数时使用?
class Test {
public:
Test() {
std::cout << "ctor" << std::endl;
}
Test(const Test&) {
std::cout << "copy ctor" << std::endl;
}
Test(const Test&&) {
std::cout << "move ctor" << std::endl;
}
};
template<typename Arg>
void pass(Arg&& arg) {
// use arg..
return;
}
template<typename Arg, typename ...Args>
void pass(Arg&& arg, Args&&... args)
{
// use arg...
return pass(args...); // why should I use std::forward<Arg>(args)... ?
}
int main(int argc, char** argv)
{
pass(std::move<Test>(Test()));
return 0;
} …Run Code Online (Sandbox Code Playgroud) 考虑简单的代码(免责声明:这是一个noobie问题):
template<typename T> struct foo
{
foo(const T&);
foo(T&& ctorArguement):ptrToSomeType(new someType(std::forward<T&&>(ctorArguement))){}
^^
std::unique_ptr<someType> ptrToSomeType;
}
Run Code Online (Sandbox Code Playgroud)
与此相比:
template<typename T> struct foo
{
foo(const T&);
foo(T&& ctorArguement):ptrToSomeType(new someType(std::forward<T>(ctorArguement))){}
^^
std::unique_ptr<someType> ptrToSomeType;
}
Run Code Online (Sandbox Code Playgroud)
我想我应该使用std :: move但我特别想知道这两个案例.两个版本完全相同,还是一个比另一个更好?