2-0
編寫一個一元元函數(shù) add_const_ref<T>, 如果T是一個引用類型榨婆,就返回T馆揉,否則返回T const&格带。
#include <iostream>
template <bool b, typename T>
struct add_const_reference;
template <typename T>
struct add_const_ref
{
using type = typename add_const_reference<std::is_reference<T>::value, T>::type;
};
template <typename T>
struct add_const_reference<false, T>
{
using type = typename std::add_lvalue_reference<typename std::add_const<T>::type>::type;
};
template <typename T>
struct add_const_reference<true, T>
{
using type = typename std::remove_reference<T>::type;
};
int main(int argc, char *argv[])
{
using a = int&;
using b = int;
std::cout << std::is_same<b, typename add_const_ref<a>::type>::value << std::endl;
using c = int const&;
std::cout << std::is_same<c, typename add_const_ref<b>::type>::value << std::endl;
}
2-1
編寫一個三元元函數(shù) replace_type<c, x, y>准脂, 讓他接受一個任意的復合類型c作為其第一個參數(shù)邪媳,并將c中出現(xiàn)的所有type x替換為y捐顷;