C++0x尝鲜:perfect forwarding(完美转发)

  代码1
#include <iostream>
#include <string>
using namespace std;

void inner(string&) {
	cout << "inner(string&)" << endl;
}

void inner(const string&) {
	cout << "inner(const string&)" << endl;
}

template <typename T> void outer(T& t) {
	inner(t);
}

template <typename T> void outer(const T& t) {
	inner(t);
}

string strange() {
	return "strange()";
}

const string charm() {
	return "charm()";
}

int main() {
string up("up");
const string down("down");

cout << "Directly calling inner()." << endl;
	inner(up);
	inner(down);
	inner(strange());
	inner(charm());

	cout << endl << "Calling outer()." << endl;
	outer(up);
	outer(down);
	outer(strange());
	outer(charm());
}

//Directly calling inner().
//inner(string&)
//inner(const string&)
//inner(const string&)
//inner(const string&)
//
//Calling outer().
//inner(string&)
//inner(const string&)
//inner(const string&)
//inner(const string&)

代码2
#include <iostream>
#include <ostream>
#include <string>
using namespace std;
 
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()";
}
 
int main() {
    string up("up");
    const string down("down");
 
    quark(up);
    quark(down);
    quark(strange());
    quark(charm());
}

//t: up
//T: string&
//T&&: string&
//
//t: down
//T: const string&
//T&&: const string&
//
//t: strange()
//T: string
//T&&: string&&
//
//t: charm()
//T: const string
//T&&: const string&&

代码3
#include <iostream>
#include <string>
using namespace std;

template <typename T> struct Identity {
	typedef T type;
};

template <typename T> T&& Forward(typename Identity<T>::type& t) {
	return static_cast<T&&>(t);
}

void inner(string&) {
	cout << "inner(string&)" << endl;
}

void inner(const string&) {
	cout << "inner(const string&)" << endl;
}

void inner(string&&) {
	cout << "inner(string&&)" << endl;
}

void inner(const string&&) {
	cout << "inner(const string&&)" << endl;
}

template <typename T> void outer(T&& t) {
	inner(Forward<T>(t));
}

string strange() {
	return "strange()";
}

const string charm() {
	return "charm()";
}

int main() {
	string up("up");
	const string down("down");

	cout << "Directly calling inner()." << endl;
	inner(up);
	inner(down);
	inner(strange());
	inner(charm());

	cout << endl << "Calling outer()." << endl;
	outer(up);
	outer(down);
	outer(strange());
	outer(charm());
}

//Directly calling inner().
//inner(string&)
//inner(const string&)
//inner(string&&)
//inner(const string&&)
//
//Calling outer().
//inner(string&)
//inner(const string&)
//inner(string&&)
//inner(const string&&)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值