与此 相关的问题。通过跟踪slt_pair. h
and move. h
,似乎Clang和G++之间的区别在于内部。我试图模拟对象的分配(pair.first)与实现相同,输出与Clangstd_pair.h
输出相同,它是合理的输出,但是为什么在使用对时它会发生变化。
#include <iostream>
struct Foo {
Foo() {
std::cout << "default" << std::endl;
}
Foo(Foo& f2) {
std::cout << "non-const" << std::endl;
}
Foo(const Foo& f2) {
std::cout << "const" << std::endl;
}
};
// static_cast Foo lvalue to rvalue
Foo cast1(Foo foo){
return static_cast<Foo&&>(foo);
}
// same : check weather foo and Foo are the same type
Foo cast2(Foo foo){
return static_cast<typename std::remove_reference<Foo>::type&&>(foo);
}
int main() {
Foo T1; // pair Foo object
std::cout<<"Start"<<std::endl;
std::cout<<std::endl;
// (&&) rvalue casting
T1 = cast2(Foo()); // First pair object
std::cout << std::endl;
Foo const foo = T1;// Second pair object
}
Clang如何处理转换lvalue
以及rvalue
这些不同输出的真正原因。
任何意见都非常感谢,谢谢。
更新:我在接受的评论部分得到了令人满意的答案。