c++ - 类的模板化包装函数(例如 std::make_pair())` 是否被认为很慢?

标签 c++ performance templates copy c++11

我正在使用std::make_pair()对于这个例子,因为几乎所有 C++ 程序员都应该熟悉它,但更一般地说,我想知道它使用的模式。

我突然想到,虽然我很享受std::make_pair()的便利它为每个参数创建一个“额外”拷贝,因为它创建一个对并按值返回它。如果我然后使用它插入到 STL 容器中,这意味着实际上每个参数总共被复制 3 次...我编写了这个代码片段来说明(以及一些改进它而不失去太多便利性的尝试):

#include <iostream>
#include <utility>
#include <list>

using namespace std;
// C++11 only:
#define MAKE_PAIR(a,b) pair<decltype(a),decltype(b)>((a),(b))

class A {
public:
  A () { }
  A (const A& a) {
    cout << "\tCopy constructor called" << endl;
  }
};

int main()
{
  list<pair<int,A> > l;

  cout << "Using std::make_pair()" << endl;
  l.push_back(make_pair(10,A()));

  cout << "Using MAKE_PAIR()" << endl;
  l.push_back(MAKE_PAIR(10,A()));

  typedef pair<int, A> my_pair;
  cout << "Using a typedef" << endl;
  l.push_back(my_pair(10,A()));
}

产生输出:

Using std::make_pair()
    Copy constructor called
    Copy constructor called
    Copy constructor called
Using MAKE_PAIR()
    Copy constructor called
    Copy constructor called
Using a typedef
    Copy constructor called
    Copy constructor called

我意识到这里还有一些其他拷贝可能可以被消除(或者更确切地说减少为指针/智能指针拷贝),例如通过使用 A * 或成对的智能指针,然后自己分配它。

宏的想法(需要 C++11)对我来说似乎很有趣,尽管我知道很多人不喜欢宏。 typedef 也可以正常工作,但是您必须为每组模板参数创建一个单独的 typedef,因此它比每次显式指定模板参数更方便,但它仍然不如包装函数那么好。

我想知道是否有人真的避免 make_pair()出于这个原因在实践中? C++/C++11 是否提供任何其他有趣的解决方案?

我喜欢围绕构造函数创建模板化函数包装器的想法,以便我们可以推断出模板参数,但我并不热衷于因此而导致运行时影响。

最佳答案

在我的编译器中,无论有没有优化,额外的复制构造函数都被优化掉了。

[10:53pm][wlynch@orange /tmp] c++ --version
Apple clang version 3.1 (tags/Apple/clang-318.0.45) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin11.3.0
Thread model: posix
[10:54pm][wlynch@orange /tmp] c++ -O0 -std=gnu++11 foo.cc -o foo
[10:54pm][wlynch@orange /tmp] ./foo
Using std::make_pair()
    Copy constructor called
    Copy constructor called
Using MAKE_PAIR()
    Copy constructor called
    Copy constructor called
Using a typedef
    Copy constructor called
    Copy constructor called

如果我添加参数-fno-elide-constructors,那么我们会看到额外的构造函数。

[10:57pm][wlynch@orange /tmp] c++ -std=gnu++11 -fno-elide-constructors foo.cc -o foo
[10:57pm][wlynch@orange /tmp] ./foo
Using std::make_pair()
    Copy constructor called
    Copy constructor called
    Copy constructor called
    Copy constructor called
Using MAKE_PAIR()
    Copy constructor called
    Copy constructor called
Using a typedef
    Copy constructor called
    Copy constructor called

C++ 规范在 2003 规范的 [class.copy.15] 中对跳过复制构造函数有这样的规定:

When certain criteria are met, an implementation is allowed to omit the copy construction of a class object, even if the copy constructor and/or destructor for the object have side effects. In such cases, the implemen- tation treats the source and target of the omitted copy operation as simply two different ways of referring to the same object, and the destruction of that object occurs at the later of the times when the two objects would have been destroyed without the optimization.) This elision of copy operations is permitted in the following circumstances (which may be combined to eliminate multiple copies):

  • in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object with the same cv-unqualified type as the function return type, the copy operation can be omitted by constructing the automatic object directly into the function’s return value
  • when a temporary class object that has not been bound to a reference (12.2) would be copied to a class object with the same cv-unqualified type, the copy operation can be omitted by constructing the temporary object directly into the target of the omitted copy

关于c++ - 类的模板化包装函数(例如 std::make_pair())` 是否被认为很慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9578047/

相关文章:

c++ - 概念可以与CRTP习语一起使用吗?

c++ - 当我将参数传递给要绘制的函数时,openGL 不绘制网格

sql - 连接具有数百万行的表

performance - 从集合中选择随机元素,比线性时间快(Haskell)

c++ - 确保可变参数模板不包含重复项

c++ - 尝试获取 vector 迭代器的值时出现总线错误 10

c++ - 如何返回一个不透明的句柄(void* 或 dword),它可以转换回存储在 boost::interprocess 映射中的值元素?

python - 为什么 numpy 和 scipy exp() 比 log() 更快?

c++ - 在模板函数 C++ 中的 F&&,Args &&... 参数之后添加另一个函数作为参数

c++ - 使用基类指针调用带有派生类参数的模板化函数