右值右值例子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 &&) { /* 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);
}
#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 &&) { /* 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
- 右值右值例子universal references
- Item24 Distinguish universal references from rvalues reference
- Item26 Avoid overloading on universal references
- Universal References in C++11—Scott Meyers
- Item27 Familiarize yourself with alternatives to overloading on universal references
- jdk8特性例子Method and Constructor References
- References
- references
- References....
- References
- References
- Android-Universal-Image-Loader使用例子
- Item25 Use std::move on rvalue reference, std::forward on universal references
- Weak references & Soft references
- Coding References
- About References!
- C# References
- 3. References
- wdcp 平台soap扩展安装
- WEB开发中的页面跳转方法总结
- tensorflow实战系列(三)一个完整的例子
- 国家民委教育科技司副司长周晓梅一行视察汪国新湖北诗书画院
- 实验三作业
- 右值右值例子universal references
- 【NOI2017模拟.4.1】Shoes【DP决策单调性,主席树,分治】
- CSS position详解
- 【压箱底】莫名其妙收藏了几本书
- [深入RxBus]:支持Sticky事件
- 优化TTFB
- “金猴献瑞”——中国当代书画名家八人展在京举行,龚建明、石光树、李玉成、戴泽、萧瀚、汪国新、薛林兴、丁国成、潘慧敏、陈北文、王润等出席
- oralce中的5种约束
- Java复习-并发编程之lock