小编Nia*_*all的帖子

为什么在C++ 11/14中没有std :: move_if_noexcept对应std :: forward?

我曾经看过斯科特迈尔斯上GoingNative2013谈话'的有效C++ 11月14日采样器’,并解释说,他使用的std::move_if_noexcept.

所以我认为还应该std::forward_if_noexcept保证例外安全forward吗?为什么标准库中没有这样的内容?还有其他可能保证吗?

c++ noexcept perfect-forwarding c++11

12
推荐指数
1
解决办法
649
查看次数

可以删除(或默认)非特殊C++成员函数吗?

很多关注已收到= default,并= delete针对特殊的成员(默认构造函数,析构函数,复制和移动).可以= default= delete其他功能一起使用; 会员功能,免费功能和运营商等?

我可以理解,= default在特殊成员之外可能不允许这样做; 因为它基本上是说使用编译器生成的默认值.在编译器能够生成默认值之前,需要明确定义默认值.据我所知,只有特殊成员才有这些预定义的默认值.

那怎么样= delete; 它基本上是说声明了函数,但是实现没有明确定义.

  • 可以= delete用于特殊成员以外的功能吗?
  • 可以使用哪些功能类型(成员,非成员,运营商等)?
  • 或者相反,= delete禁止使用何处(或何时)?它的使用有任何限制吗?

c++ c++11

11
推荐指数
1
解决办法
766
查看次数

为什么std :: string :: append()的功能不如std :: string :: operator +()?

我注意到了

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方法的包装器,但似乎并非如此.

c++ string stl

11
推荐指数
2
解决办法
894
查看次数

为什么C++左值对象不能绑定到右值引用(&&)?

移动语义的想法是,您可以从另一个临时对象(由右值引用引用)中获取所有内容,并将"所有内容"存储在对象中.这有助于避免在单个构造事物足够的情况下进行深度复制 - 因此您可以在rvalue对象中构造事物,然后将其移动到长寿命对象中.

为什么C++不允许将左值对象绑定到右值引用?两者都允许我更改引用的对象,因此在访问引用对象的内部方面对我没有任何区别.

我能猜到的唯一原因是函数重载模糊问题.

c++ rvalue-reference move-semantics c++11

11
推荐指数
1
解决办法
3561
查看次数

返回const引用或引用的方法是否会导致内存泄漏?

如果从方法返回引用可能导致内存泄漏,我非常好奇.以下是示例情况.

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_valtest_vect从堆栈时遭到移除test_run退出.是test_vect或者test_val被删除从而导致对象被破坏?

c++ memory-leaks

10
推荐指数
2
解决办法
952
查看次数

移动语义并完善转发差异

我已经从这个问题得到了什么样的移动语义: 什么是移动语义?

但是我仍然没有得到与移动语义相关的完美转发.

有人可以用简单的英语和一个简单的例子解释完美的转发意味着什么?

c++ move-semantics perfect-forwarding c++11

9
推荐指数
1
解决办法
2267
查看次数

子类中的默认移动构造函数

在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)

c++ move-semantics c++11

9
推荐指数
1
解决办法
2053
查看次数

C++ program unexpectedly blocks / throws

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)

c++ multithreading c++11

9
推荐指数
1
解决办法
473
查看次数

禁用移动构造函数

我想在类中禁用移动构造函数.而不是移动,我想基于复制构造函数.当我尝试编写此代码时:

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++ move c++11

9
推荐指数
2
解决办法
2912
查看次数

如果我为一个类编写运算符new和delete,我是否必须编写所有重载?

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)

c++ memory-management new-operator delete-operator

9
推荐指数
1
解决办法
698
查看次数