小编Ell*_*tch的帖子

通过调用移动赋值运算符实现移动构造函数

MSDN文章如何:编写移动构造函数具有以下建议.

如果为类提供移动构造函数和移动赋值运算符,则可以通过编写移动构造函数来调用移动赋值运算符来消除冗余代码.以下示例显示了调用移动赋值运算符的移动构造函数的修订版本:

// Move constructor.
MemoryBlock(MemoryBlock&& other)
   : _data(NULL)
   , _length(0)
{
   *this = std::move(other);
}
Run Code Online (Sandbox Code Playgroud)

这个代码是通过双重初始化MemoryBlock的值来实现低效的,还是编译器能够优化掉额外的初始化?我是否应该通过调用移动赋值运算符来编写移动构造函数?

c++ move-constructor move-semantics c++11

17
推荐指数
2
解决办法
8994
查看次数

C++最早的未定义行为可以表现出来的是什么?

我知道未定义的行为可能会导致任何事情,这使得任何包含UB的程序都可能毫无意义.我想知道是否有任何方法可以确定程序中最早的一点,即未定义的行为可能会导致问题.这是一个例子来说明我的问题.

void causeUndefinedBehavior()
{
   //any code that causes undefined behavior
   //every time it is run
   char* a = nullptr;
   *a;
}


int main()
{
 //code before call
 //...
 causeUndefinedBehavior();
 //code after call
 //...
}
Run Code Online (Sandbox Code Playgroud)

根据我的理解,可能引发未定义行为的可能时间(不一定表现出来)是:

  1. 何时causeUndefinedBehavior()编译.
  2. 何时main()编译.
  3. 在程序运行时.
  4. 当时causeUndefinedBehavior()执行.

或者,对于每个案例和每个实现,未定义的行为都会被完全不同?

另外,如果我注释掉causeUndefinedBehavior()调用的行,是否会消除UB,或者它是否仍然在程序中,因为编译了包含UB的代码?

c++ undefined-behavior

10
推荐指数
1
解决办法
316
查看次数

检查指针是否为空是否安全,然后在同一if语句中取消引用它?

如果传入空指针,以下代码是否安全?

if(ptr && *ptr == value)
{
   //do something
}
Run Code Online (Sandbox Code Playgroud)

检查的顺序是否重要?如果我改成它,它会起作用吗?

if(*ptr == value && ptr)
{
   //do something
}
Run Code Online (Sandbox Code Playgroud)

c c++ pointers conditional-statements

10
推荐指数
1
解决办法
3267
查看次数

C++在Vector中使用不可分配的对象

我想在一个对象列表中存储std::vector,但对象包含一个引用,无法分配.但是,我可以复制构造对象.

我能想到的唯一选择是使用指针来包装对象并在需要分配指针时重新设置指针,但是这样做的语法会大大降低可读性,尤其是在使用迭代器时,我宁愿选择其他方法.

不起作用:

std::vector<MyObject> myVector;
//fill the vector
//...
myVector[1] = object1;
Run Code Online (Sandbox Code Playgroud)

智能指针牺牲了可读性:

std::vector<std::unique_ptr<MyObject>> ptrVector;
//fill the vector
//...
ptrVector[1] = std::unique_ptr<MyObject>(new MyObject(object1));
Run Code Online (Sandbox Code Playgroud)

有没有其他方法可以使用不可分配的对象std::vector

c++ containers reference vector

5
推荐指数
1
解决办法
302
查看次数

编译时断言接口中的可选属性在类中不为 null

在 Javascript 中,我喜欢将 an 传递options object给 a constructor ,并用默认值填充我期望的任何缺失属性:

function foo(options) {
   if(options.a == undefined) {
      options.a = 1;
   }
   if(options.b == undefined) {
      options.b = 'hello';
   }
   this.options = options;
}

new foo({a: 10});
Run Code Online (Sandbox Code Playgroud)

尝试在 Typescript 中实现此模式,我有:

interface Options {
    a?: number;
    b?: string;
}

class Foo {
   options: Options;
   constructor(options: Options) {
      if(options.a == undefined) {
         options.a = 1;
      }
      if(options.b == undefined) {
         options.b = 'hello';
      }
      this.options = options;
   }
}
Run Code Online (Sandbox Code Playgroud)

当我options在 …

interface optional-parameters typescript

5
推荐指数
1
解决办法
2135
查看次数

get char on screen

I've looked through the NCurses function list, and I can't seem to find a function that returns the characters already printed on the screen. Is there an accessible value for the char stored in each character cell? If not, is there a similar function in the Windows terminal?

我想用它来用a不同的字符或新的属性替换屏幕上某个值(例如:所有的)的所有字符。

c++ ncurses

3
推荐指数
1
解决办法
2348
查看次数

Vim:基于当前FileType的文件扩展名为gf

我正在尝试设置我的.vimrc所以gf将通过尝试打开与当前FileType具有相同扩展名的文件,自动处理缺少文件扩展名的路径.

换句话说,我想要类似的东西:

autocmd FileType <filetype> setl suffixesadd+=<exts>
Run Code Online (Sandbox Code Playgroud)

where <exts>是与当前关联的所有文件扩展名的列表<filetype>.

例如,我的filetype.vim定义文件类型"的JavaScript"与名称的文件*.js,*.javascript,*.es,*.jsx,和*.json,所以每当我在编辑一个javascript缓冲区,如果光标的道路上./index,行驶gf应尽量打开./index.js.如果该文件不存在则应该尝试./index.javascript,依此类推.如果文件类型是蟒蛇,它应该尝试./index.py,./index.pyw等等.

我非常确定上面的autocmd应该产生预期的行为,如果我只为每个FileType运行它,但我不知道如何做到这一点.

vim

3
推荐指数
1
解决办法
959
查看次数

C++ std :: reference_wrapper如何隐式转换为引用?

我最近开始使用这std::reference_wrapper门课.当替换原始引用的某些用法时,我注意到我没有必要使用该get()函数将reference_wrappers作为参数传递给采用普通引用的函数.

void foo(const T& a);
//...
T obj;
std::reference_wrapper<const T> ref(obj);
foo(ref);  //works!
//foo(ref.get()); also works, but I expected that it would be required
Run Code Online (Sandbox Code Playgroud)

std::reference_wrapper当它传递给函数时,如何隐式转换为原始引用?

c++ reference implicit-conversion c++11 reference-wrapper

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