模板笔记7 字符串作为模板参数(特别注意)

#include <iostream>
#include <string>
#include <typeinfo>

#define HH 1

#ifdef HH
template<typename T>
inline T const& max(T const& a, T const& b)
{
return a < b ? b : a;
}

#elif

template<typename T>
inline T max(T a, T b)
{
return a < b ? b : a;
}

#endif

template<typename T>
void ref (T const& x)
{
std::cout << "ref " << typeid(x).name() << std::endl;
}
template<typename T>
void nonref (T x)
{
std::cout << "nonref " << typeid(x).name() << std::endl;
}
int main()
{
std::string s;
std::cout << ::max("apple", "peach") << std::endl;
//::max("apple", "tomato");//如果字符串长度相同,实例化后的类类型才相同。否则报错。这种情况,重载解决。如果不重载也要调用长度不一样的,

//去掉#define HH 1 ,非引用类型的参数,在实参演绎的过程中,会出现数组到指针的类型转换
//::max("apple", s);
::ref("hello");
::nonref("hello");
}

原文地址:https://www.cnblogs.com/xpylovely/p/12519027.html