gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 01:54:29 24 4
gpt4 key购买 nike

我在这个例子中使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com