The question related to this one. By tracing slt_pair. h
and move. h
, it's seems that the difference between Clang and G++ is internally. I have tried to simulate the assignment of the object (pair.first) as same as the implementation of std_pair.h
, the output is same as Clang output it's reasonable output, but why when using pairs it's changes.
#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
}
How Clang deals with conversion lvalue
to rvalue
and what the real reason for these different outputs.
Any opinion is highly appreciated, Thanks.
Update: I have got a satisfying answers on the comments section of the accepted one.
std::pair
implementations, not compiler behavior. "the output is same as Clang" "what the real reason for these different outputs" Do you get the same output for GCC and Clang, or different ones? If it's the same, what exactly is the question? – Suggestible