简体   繁体   English

为什么我不应该为我的类型提供 forward 和 move 版本?

[英]Why I should not provide a forward and move versions for my types?

I've used std::swap , std::move and std::forward but I've been recommended not to overload the latter two ones because they already have paramters that can hold any tylpe of value for example a forearding reference.我使用了std::swapstd::movestd::forward但我被建议不要重载后两个,因为它们已经具有可以保存任何类型值的参数,例如前向引用。 So I should fully-quallify the call to those two functions:所以我应该完全限定对这两个函数的调用:

std::move(x);
std::forward<T&&>(x);

And I've read that I can provide my own version of swap and thus I only add a usung declaration for the standard library one then call swap without qualifying it (unqualified lookup):而且我已经读到我可以提供我自己的swap版本,因此我只为标准库添加一个 usung 声明,然后调用swap而不对其进行限定(非限定查找):

using std::swap;// in case my class doesn't provide a swap version

swap(x, y); // where x, y are of class type. so don't use std::swap directly
  • So why I should not provide a version of std::move and std::forward for my types too?那么为什么我不应该为我的类型提供std::movestd::forward版本呢?

  • Does this mean if I provided versions, there is no benefit because the library's are better like proving a sort function whereas the library's is so far better?这是否意味着如果我提供版本,则没有任何好处,因为库更好地证明了排序 function 而库的更好?

You are only allowed to provide specializations of std functions when the standard permits you to.只有在标准允许时,您才可以提供std函数的特化。 No such permission is given for std::move or std::forward .没有为std::movestd::forward提供此类许可。 Doing so makes your program ill-formed, no diagnostic required.这样做会使您的程序格式错误,不需要诊断。

std::swap you should only specialize (not overload.) when the default implementation has efficiency problems: The default implementation is a basically: std::swap你应该只在默认实现存在效率问题时专门化(而不是重载。):默认实现基本上是:

void std::swap( T& lhs, T& rhs ) {
  auto tmp = std::move(lhs);
  lhs = std::move(rhs);
  rhs = std::move(tmp);
}

The most common case is when your type needs to allocate memory when it is "empty".最常见的情况是当您的类型为“空”时需要分配 memory。 A more efficient swap could avoid such an allocation.更有效的交换可以避免这种分配。

But for most types, this isn't a problem.但对于大多数类型,这不是问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 只要我可以为我的类型提供哈希 function 为什么我需要为我的类型专门化 std::hash ? - As long as I can provide a hasher function for my types why I need to specialize std::hash for my types? 什么时候应该使用前进和移动? - When should I use forward and move? 我什么时候应该为我的班级提供一个析构函数? - When should I provide a destructor for my class? 我为什么要使用std :: forward? - Why should I use std::forward? 为什么要使用2x大小相同的图标版本将应用程序部署到Mac OS - Why should I have 2x versions of icons of the same sizes for deploying my applications to Mac OS 我应该在移动ctors /赋值运算符中使用std :: move或std :: forward吗? - Should I use std::move or std::forward in move ctors/assignment operators? 为什么我应该明确地将 typename 传递给 std::forward? - Why should I explicitly pass typename to std::forward? 为什么我要删除移动构造函数并在单例中移动赋值运算符? - Why should I delete move constructor and move assignment operator in a singleton? 为什么std :: move采用前向引用? - Why does std::move take a forward reference? 我应该如何将长函数移出MainWindow(QT) - How should I move my long functions out of MainWindow (QT)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM