我曾经看过斯科特迈尔斯上GoingNative2013谈话'的有效C++ 11月14日采样器’,并解释说,他使用的std::move_if_noexcept.
所以我认为还应该std::forward_if_noexcept保证例外安全forward吗?为什么标准库中没有这样的内容?还有其他可能保证吗?
很多关注已收到= default,并= delete针对特殊的成员(默认构造函数,析构函数,复制和移动).可以= default和= delete其他功能一起使用; 会员功能,免费功能和运营商等?
我可以理解,= default在特殊成员之外可能不允许这样做; 因为它基本上是说使用编译器生成的默认值.在编译器能够生成默认值之前,需要明确定义默认值.据我所知,只有特殊成员才有这些预定义的默认值.
那怎么样= delete; 它基本上是说声明了函数,但是实现没有明确定义.
= delete用于特殊成员以外的功能吗?= delete禁止使用何处(或何时)?它的使用有任何限制吗?我注意到了
std::string str;
str += 'b'; // works
str.append('b'); // does not work
str.append(1, 'b'); // works, but not as nice as the previous
Run Code Online (Sandbox Code Playgroud)
是否有任何理由说明该append方法不支持附加单个字符?我假设它operator+=实际上是该append方法的包装器,但似乎并非如此.
移动语义的想法是,您可以从另一个临时对象(由右值引用引用)中获取所有内容,并将"所有内容"存储在对象中.这有助于避免在单个构造事物足够的情况下进行深度复制 - 因此您可以在rvalue对象中构造事物,然后将其移动到长寿命对象中.
为什么C++不允许将左值对象绑定到右值引用?两者都允许我更改引用的对象,因此在访问引用对象的内部方面对我没有任何区别.
我能猜到的唯一原因是函数重载模糊问题.
如果从方法返回引用可能导致内存泄漏,我非常好奇.以下是示例情况.
class example
{
public:
vector<int> & get_vect()
{
return vect;
}
int & get_num()
{
return num;
}
private:
vector<int> vect;
int num;
};
void test_run(example & input)
{
int & test_val = input.get_num();
vector<int> & test_vect = input.get_vect();
}
int main()
{
example one;
test_run(one);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是什么时候test_val和test_vect从堆栈时遭到移除test_run退出.是test_vect或者test_val被删除从而导致对象被破坏?
在C++ 11中,如果基类定义了自己的移动(复制)构造函数(赋值运算符),它的子类是否需要定义自己的移动(复制)构造函数(赋值运算符),其中调用基类的相应构造函数/运算符是否明确调用?
每次都清楚地定义构造函数,析构函数,移动/复制构造函数(赋值运算符)是一个好主意吗?
struct Base {
Base() {}
Base(Base&& o);
};
struct Sub : public Base {
Sub(Sub&& o) ; // Need I do it explicitly ? If not,what the compiler will do for me
};
Run Code Online (Sandbox Code Playgroud) I'm learning about mutexes in C++ and have a problem with the following code (taken from N. Josuttis' "The C++ Standard Library").
I don't understand why it blocks / throws unless I add this_thread::sleep_for in the main thread (then it doesn't block and all three calls are carried out).
The compiler is cl.exe used from the command line.
#include <future>
#include <mutex>
#include <iostream>
#include <string>
#include <thread>
#include <chrono>
std::mutex printMutex;
void print(const std::string& s)
{
std::lock_guard<std::mutex> lg(printMutex);
for …Run Code Online (Sandbox Code Playgroud) 我想在类中禁用移动构造函数.而不是移动,我想基于复制构造函数.当我尝试编写此代码时:
class Boo
{
public:
Boo(){}
Boo(const Boo& boo) {};
Boo(Boo&& boo) = delete;
};
Boo TakeBoo()
{
Boo b;
return b;
}
Run Code Online (Sandbox Code Playgroud)
在编译期间我收到错误:
错误C2280:'Boo :: Boo(Boo &&)':尝试引用已删除的函数
如何禁用移动构造函数并强制复制?
C++ Reference页面列出了全局新运算符的 8个特定于类的重载.其中四个是为2017版C++添加的.
特定于类的分配函数
void* T::operator new ( std::size_t count );
void* T::operator new[]( std::size_t count );
void* T::operator new ( std::size_t count, std::align_val_t al ); // (since C++17)
void* T::operator new[]( std::size_t count, std::align_val_t al ); // (since C++17)
Run Code Online (Sandbox Code Playgroud)
特定于类的放置分配功能
void* T::operator new ( std::size_t count, user-defined-args... );
void* T::operator new[]( std::size_t count, user-defined-args... );
void* T::operator new ( std::size_t count,
std::align_val_t al, user-defined-args... ); // (since C++17)
void* T::operator new[]( std::size_t count,
std::align_val_t …Run Code Online (Sandbox Code Playgroud)