问题描述
我想验证以下优化是否按预期工作:
- RVO
- 已命名为RVO
- 按值传递参数时复制elision
:
#include< algorithm>
#include< cstddef>
#include< iostream>
#include< vector>
struct Foo {
Foo(std :: size_t length,char value):data(length,value){}
Foo(const Foo& rhs ):data(rhs.data){std :: cout< *** COPY ***<< std :: endl; }
Foo& operator =(Foo rhs){
std :: cout< *** ASSIGNMENT ***<< std :: endl;
std :: swap(data,rhs.data); //可能很贵,忽略这个请
return * this;
}
〜Foo(){}
std :: vector< char>数据;
};
Foo TestRVO(){return Foo(512,'r'); }
Foo TestNamedRVO(){Foo result(512,'n');返回结果; }
void PassByValue(Foo inFoo){}
int main()
{
std :: cout< \\\
Test RVO:< std :: endl;
Foo rvo = TestRVO();
std :: cout<< \\\
Test named RVO:< std :: endl;
Foo named_rvo = TestNamedRVO();
std :: cout<< \\\
Test PassByValue:< std :: endl;
Foo foo(512,'a');
PassByValue(foo);
std :: cout<< \\\
Test assignment:< std :: endl;
Foo f(512,'f');
Foo g(512,'g');
f = g;
}
我用优化启用编译它:
$ g ++ -o test -O3 main.cpp; ./test
这是输出:
测试RVO:
测试命名为RVO:
测试PassByValue:
*** COPY ***
测试作业:
*** COPY ***
***分配***
根据输出RVO和命名RVO按预期工作。但是,对于赋值运算符和调用 PassByValue
时不执行复制精度。
不允许复制用户定义的复制构造函数? (我知道RVO是明确允许的标准,但我不知道关于复制elision当传递值。)有没有一种方法来验证复制elision没有定义复制构造函数?
使用复制构造函数的方式不能省略,因为复制的对象在调用后仍然存在。
PassByValue(Foo(512,'a'))
所有优化都是允许的,但不是必需的,所以每个编译器都可以决定什么,将做。
I wanted to verify the if the following optimizations work as expected:
- RVO
- Named RVO
- Copy elision when passing an argument by value
So I wrote this little program:
#include <algorithm>
#include <cstddef>
#include <iostream>
#include <vector>
struct Foo {
Foo(std::size_t length, char value) : data(length, value) { }
Foo(const Foo & rhs) : data(rhs.data) { std::cout << "*** COPY ***" << std::endl; }
Foo & operator= (Foo rhs) {
std::cout << "*** ASSIGNMENT ***" << std::endl;
std::swap(data, rhs.data); // probably expensive, ignore this please
return *this;
}
~Foo() { }
std::vector<char> data;
};
Foo TestRVO() { return Foo(512, 'r'); }
Foo TestNamedRVO() { Foo result(512, 'n'); return result; }
void PassByValue(Foo inFoo) {}
int main()
{
std::cout << "\nTest RVO: " << std::endl;
Foo rvo = TestRVO();
std::cout << "\nTest named RVO: " << std::endl;
Foo named_rvo = TestNamedRVO();
std::cout << "\nTest PassByValue: " << std::endl;
Foo foo(512, 'a');
PassByValue(foo);
std::cout << "\nTest assignment: " << std::endl;
Foo f(512, 'f');
Foo g(512, 'g');
f = g;
}
And I compiled it with optimizations enabled:
$ g++ -o test -O3 main.cpp ; ./test
This is output:
Test RVO:
Test named RVO:
Test PassByValue:
*** COPY ***
Test assignment:
*** COPY ***
*** ASSIGNMENT ***
According to the output RVO and named RVO work as expected. However, copy elision is not performed for the assignment operator and when calling PassByValue
.
Is copy elision not allowed on user defined copy-constructors? (I know that RVO is explicitly allowed by the standard but I don't know about copy elision when passing by value.) Is there a way to verify copy elision without defining copy constructors?
The way you use the copy constructor it can not be elided, as the copied object still exists after the call.
If you try it this way, it might work better:
PassByValue(Foo(512, 'a'));
All optimizations are allowed but not required, so it is up to each compiler to decide what it can and will do.
这篇关于复印检查的条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!