Categories
不学无术

C++模板元编程 习题: add_const_ref

要学的东西还有很多啊…
习题2-0. 编写一个一元元函数add_const_ref<T>,如果T是一个引用类型,就返回T,否则返回T const&.

#include "add_const_ref.h"
int main()
{
	int a = 5;
	add_const_ref<int>::type c = a;
	add_const_ref<int&>::type d = a;
	return 0;
}

 

#ifndef ADD_CONST_REF
#define ADD_CONST_REF
template <class T>
struct add_const_ref;
template<class T>
struct add_const_ref
{
	typedef T const& type;
};
template<class T>
struct add_const_ref<T&>
{
	typedef T type;
};
#endif

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.