右值右值例子universal references

来源:互联网 发布:sql 每小时统计 编辑:程序博客网 时间:2024/06/07 07:21
#include"stdafx.h"
#include <iostream>
#include <memory>
#include <utility>
#include <array>
#include <iostream>
#include <iostream>
#include <string>
#include <ostream>


using namespace std;


struct A {
A(int&& n) { std::cout << "rvalue overload, n=" << n << "\n"; }
A(int& n)  { std::cout << "lvalue overload, n=" << n << "\n"; }
};


class B {
public:
template<class T1, class T2, class T3>
B(T1&& t1, T2&& t2, T3&& t3) :
a1_{ std::forward<T1>(t1) },
a2_{ std::forward<T2>(t2) },
a3_{ std::forward<T3>(t3) }
{
}


private:
A a1_, a2_, a3_;
};


template<class T, class U>
std::unique_ptr<T> make_unique1(U&& u)
{
return std::unique_ptr<T>(new T(std::forward<U>(u)));
}


template<class T, class... U>
std::unique_ptr<T> make_unique(U&&... u)
{
return std::unique_ptr<T>(new T(std::forward<U>(u)...));
}


int test(int& i)
{
return i;
}


/*void _inter(int&&, int&&) {


cout << "inner(const int&, int&)" << endl;


}


void _inter(const int&&, const int&&) {


cout << "inner(const int&, int&)" << endl;


}*/




void _inter(const int&, const int&) {


cout << "inner(const int&, const int&)" << endl;


}


void _inter(int&, int&) {


cout << "inner(int&, const int&)" << endl;


}
template <typename T1, typename T2> void outer1(T1&& t1, T2&& t2) {


_inter(std::forward<T1>(t1), std::forward<T2>(t2));


}


void tinner(const int&, const int&) {


cout << "inner(const int&, const int&)" << endl;


}


template <typename T> struct Name;






template <> struct Name<string> {


static const char * get() {


return "string";


}


};






template <> struct Name<const string> {


static const char * get() {


return "const string";


}


};






template <> struct Name<string&> {


static const char * get() {


return "string&";


}


};






template <> struct Name<const string&> {


static const char * get() {


return "const string&";


}


};






template <> struct Name<string&&> {


static const char * get() {


return "string&&";


}


};






template <> struct Name<const string&&> {


static const char * get() {


return "const string&&";


}


};






template <typename T> void quark(T&& t) {


cout << "t: " << t << endl;


cout << "T: " << Name<T>::get() << endl;


cout << "T&&: " << Name<T&&>::get() << endl;


cout << endl;


}






string strange() {


return "strange()";


}






const string charm() {


return "charm()";


}


template <class _Ty> struct Identity {


Identity(){ int i = 10;
i = 200;


}
typedef _Ty type;


};


template<class _Ty>
struct Identity<_Ty&>
{ // remove reference
typedef _Ty type;
};


template<class _Ty>
struct Identity<_Ty&&>
{ // remove rvalue reference
typedef _Ty type;
};


/*template <typename T> T&& Forward(typename Identity<T>::type&& t) {


return t;


}*/


template<class _Ty> inline
_Ty&& Forward(typename Identity<_Ty>::type&& _Arg) _NOEXCEPT
{ // forward anything
static_assert(!is_lvalue_reference<_Ty>::value, "bad forward call");
return (static_cast<_Ty&&>(_Arg));
}


template <typename T> T&& Forward(typename Identity<T>::type& t) {


Identity<T>::type k = t;
T b = t;
b = 808080;
k = 808;
int i = 200;
Identity<T>::type& d = t;
Identity<T>::type& p = (Identity<T>::type&)t;
Identity<T>::type&& pp = (Identity<T>::type&&)t;
return (static_cast<T&&>(t));


}


void inner(int&, int&) {


cout << "inner(int&, int&)" << endl;


}






void inner(int&, const int&) {


cout << "inner(int&, const int&)" << endl;


}




void inner(int&&,  int&&) {


cout << "inner(int&, const int&)" << endl;


}


void inner(const int&&, const int&&) {


cout << "inner(int&, const int&)" << endl;


}


void inner(const int&, int&) {


cout << "inner(const int&, int&)" << endl;


}






void inner(const int&, const int&) {


cout << "inner(const int&, const int&)" << endl;


}






template <typename T1, typename T2> void outer(T1&& t1, T2&& t2) {


T1& a = t1;
inner(Forward<T1>(t1), Forward<T2>(t2));


}


const int geta()
{
return 3;
}
template <class _Tp> void g(_Tp &&amp)  { /* do something */ }


int main()
{
int aaa = 100;
int bbb = 200;
const int  c = 2000;
const int d = 500;
const int && e = 600;
const int && f = 600;
int bbiiii = 10;
g(aaa);
//f = e;
//int & && k = 5;
Forward<int>(5);
Forward<int&>(5);
Forward<int&>(aaa);
outer(1, 2);
outer(aaa, bbb);
inner(e, f);
outer(geta(), geta());
tinner(1, 2);
outer1(1, 2);
outer1(c, d);
outer1(e, f);
outer1(aaa, bbb);
test(aaa);
//std::forward()
auto p1 = make_unique1<A>(2); // rvalue
int i = 1;
auto p2 = make_unique1<A>(i); // lvalue


string up("up");


const string down("down");






quark(up);


quark(down);


quark(strange());


quark(charm());


std::cout << "B\n";
//auto t = make_unique<B>(2, i, 3);
}
0 0
原创粉丝点击