|
@@ -0,0 +1,38 @@
|
|
|
+#include<iostream>
|
|
|
+#include<string>
|
|
|
+template<typename T>
|
|
|
+void fun(T data=1) //当有默认参数的时候默认参数本身不能模板类型推导
|
|
|
+{
|
|
|
+ std::cout << data << std::endl;
|
|
|
+}
|
|
|
+
|
|
|
+template <typename T1,typename T2, typename R1>
|
|
|
+R1 fun1(T1 a, T2 b) //这里R1需要显示指定,因为并没有能推导的地方
|
|
|
+{
|
|
|
+ return a + b;
|
|
|
+}
|
|
|
+
|
|
|
+template <typename T1, typename T2>
|
|
|
+auto fun2(T1 a, T2 b) -> decltype(a < b ? a : b) //当然有的时候可能ab是引用会推导出引用
|
|
|
+{
|
|
|
+ return a < b ? a : b;
|
|
|
+}
|
|
|
+
|
|
|
+template <typename T1, typename T2>
|
|
|
+auto fun3(T1 a, T2 b) ->std::decay_t<decltype(a < b ? a : b)> //当然有的时候可能ab是引用会推导出引用,外层剥离引用
|
|
|
+{
|
|
|
+ return a < b ? a : b;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+int main()
|
|
|
+{
|
|
|
+ fun(1);
|
|
|
+ fun<int>();
|
|
|
+ std::cout << check<int>::value << std::endl;
|
|
|
+ std::cout << check<std::string>::value << std::endl;
|
|
|
+}
|
|
|
+
|
|
|
+
|