定义一个函数模板实现int, char, double, string类型的加法

g_style 2015-10-13 11:23:00
其中char, string 类型的加法是将后面的字符(串)加到前一个后面
...全文
1009 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2015-11-11
  • 打赏
  • 举报
回复
模板是语法糖。 语法糖越甜,编译调试查错越苦! 把有限的生命浪费在品尝/品鉴无穷多种的语法糖中,我认为不值当。
paschen 版主 2015-11-11
  • 打赏
  • 举报
回复
引用 14 楼 qq_27542855 的回复:
[quote=引用 12 楼 paschen 的回复:] [quote=引用 11 楼 qq_27542855 的回复:] [quote=引用 10 楼 paschen的回复:][quote=引用 9 楼 qq_27542855 的回复:] [quote=引用 8 楼 paschen的回复:][quote=引用 7 楼 qq_27542855 的回复:] [quote=引用 6 楼 iyomumx的回复:][quote=引用 5 楼 yshuise 的回复:] [quote=引用 2 楼 iyomumx 的回复:]
template <typename T1, typename T2>
auto Add(T1 x, T2 y)
{
    return x + y;
}

template <>
auto Add<char, char>(char x, char y)
{
    return std::string("") + x + y;
}
有点小问题,特化char,应该结果是char,而不是string[/quote] 按楼主的要求就是string啊,另外两个char相加应该是int[/quote] char类型的两个变量相加是一个字符串。 e.g char a = 'a', b='b'. 那么应该返回"ab".[/quote] 那你想把"ab"存哪? 动态分配一个char*指针来存?[/quote] 可以啊!就是用函数模板实现[/quote] 那你就这样写:

template <typename Tout, typename Tin>
Tout Add(Tin x, Tin y)
{
    return x + y;
}
 
template <>
char* Add<char*, char>(char x, char y)
{
	char* p = new char[2];
	p[0] = x;
	p[1] = y;
    return p;
}
[/quote] 不行,你的这个连编译都通过不了。[/quote] 怎么不能通过了,你编译器太老了吧[/quote] 你的可以通过吗?你试过了吗?[/quote] VS2012下没问题,10没试过
paschen 版主 2015-11-11
  • 打赏
  • 举报
回复
引用 20 楼 qq_27542855 的回复:
[quote=引用 18 楼 paschen 的回复:] 想把模板学精通要学STL,《STL源码剖析》《泛型编程与STL》
你能把你的源代码发过来吗?我怎么有点不相信你的通过了,而我的却没有通过![/quote]

template <typename Tout, typename Tin>
Tout Add(Tin x, Tin y)
{
	return x + y;
}

template <>
char* Add<char*, char>(char x, char y)
{
	char* p = new char[2];
	p[0] = x;
	p[1] = y;
	return p;
}

int main()
{
	cout<<Add<int, int>(1,2)<<endl;
	cout <<Add<char*, char>('a', 'b')<<endl; //这个输出后面是乱码,因为不是\0结尾
	getchar();
	return 0;
}
g_style 2015-11-11
  • 打赏
  • 举报
回复
引用 18 楼 paschen 的回复:
想把模板学精通要学STL,《STL源码剖析》《泛型编程与STL》
你能把你的源代码发过来吗?我怎么有点不相信你的通过了,而我的却没有通过!
g_style 2015-11-11
  • 打赏
  • 举报
回复
引用 15 楼 paschen 的回复:
[quote=引用 14 楼 qq_27542855 的回复:]
[quote=引用 12 楼 paschen 的回复:]
[quote=引用 11 楼 qq_27542855 的回复:]
[quote=引用 10 楼 paschen的回复:][quote=引用 9 楼 qq_27542855 的回复:]
[quote=引用 8 楼 paschen的回复:][quote=引用 7 楼 qq_27542855 的回复:]
[quote=引用 6 楼 iyomumx的回复:][quote=引用 5 楼 yshuise 的回复:]
[quote=引用 2 楼 iyomumx 的回复:]
template <typename T1, typename T2>
auto Add(T1 x, T2 y)
{
return x + y;
}

template <>
auto Add<char, char>(char x, char y)
{
return std::string("") + x + y;
}

有点小问题,特化char,应该结果是char,而不是string[/quote]
按楼主的要求就是string啊,另外两个char相加应该是int[/quote]
char类型的两个变量相加是一个字符串。
e.g char a = 'a', b='b'. 那么应该返回"ab".[/quote]

那你想把"ab"存哪?
动态分配一个char*指针来存?[/quote]
可以啊!就是用函数模板实现[/quote]

那你就这样写:

template <typename Tout, typename Tin>
Tout Add(Tin x, Tin y)
{
return x + y;
}

template <>
char* Add<char*, char>(char x, char y)
{
char* p = new char[2];
p[0] = x;
p[1] = y;
return p;
}
[/quote]
不行,你的这个连编译都通过不了。[/quote]

怎么不能通过了,你编译器太老了吧[/quote]


你的可以通过吗?你试过了吗?[/quote]

VS2012下没问题,10没试过[/quote]

真的?

paschen 版主 2015-11-11
  • 打赏
  • 举报
回复
想把模板学精通要学STL,《STL源码剖析》《泛型编程与STL》
g_style 2015-11-11
  • 打赏
  • 举报
回复
引用 16 楼 zhao4zhong1 的回复:
模板是语法糖。 语法糖越甜,编译调试查错越苦! 把有限的生命浪费在品尝/品鉴无穷多种的语法糖中,我认为不值当。
毕竟是一种经历吧!
g_style 2015-11-10
  • 打赏
  • 举报
回复
vs2010,我觉得不老吧!
g_style 2015-11-10
  • 打赏
  • 举报
回复
引用 12 楼 paschen 的回复:
[quote=引用 11 楼 qq_27542855 的回复:] [quote=引用 10 楼 paschen的回复:][quote=引用 9 楼 qq_27542855 的回复:] [quote=引用 8 楼 paschen的回复:][quote=引用 7 楼 qq_27542855 的回复:] [quote=引用 6 楼 iyomumx的回复:][quote=引用 5 楼 yshuise 的回复:] [quote=引用 2 楼 iyomumx 的回复:]
template <typename T1, typename T2>
auto Add(T1 x, T2 y)
{
    return x + y;
}

template <>
auto Add<char, char>(char x, char y)
{
    return std::string("") + x + y;
}
有点小问题,特化char,应该结果是char,而不是string[/quote] 按楼主的要求就是string啊,另外两个char相加应该是int[/quote] char类型的两个变量相加是一个字符串。 e.g char a = 'a', b='b'. 那么应该返回"ab".[/quote] 那你想把"ab"存哪? 动态分配一个char*指针来存?[/quote] 可以啊!就是用函数模板实现[/quote] 那你就这样写:

template <typename Tout, typename Tin>
Tout Add(Tin x, Tin y)
{
    return x + y;
}
 
template <>
char* Add<char*, char>(char x, char y)
{
	char* p = new char[2];
	p[0] = x;
	p[1] = y;
    return p;
}
[/quote] 不行,你的这个连编译都通过不了。[/quote] 怎么不能通过了,你编译器太老了吧[/quote] 你的可以通过吗?你试过了吗?
paschen 版主 2015-11-10
  • 打赏
  • 举报
回复
引用 11 楼 qq_27542855 的回复:
[quote=引用 10 楼 paschen的回复:][quote=引用 9 楼 qq_27542855 的回复:] [quote=引用 8 楼 paschen的回复:][quote=引用 7 楼 qq_27542855 的回复:] [quote=引用 6 楼 iyomumx的回复:][quote=引用 5 楼 yshuise 的回复:] [quote=引用 2 楼 iyomumx 的回复:]
template <typename T1, typename T2>
auto Add(T1 x, T2 y)
{
    return x + y;
}

template <>
auto Add<char, char>(char x, char y)
{
    return std::string("") + x + y;
}
有点小问题,特化char,应该结果是char,而不是string[/quote] 按楼主的要求就是string啊,另外两个char相加应该是int[/quote] char类型的两个变量相加是一个字符串。 e.g char a = 'a', b='b'. 那么应该返回"ab".[/quote] 那你想把"ab"存哪? 动态分配一个char*指针来存?[/quote] 可以啊!就是用函数模板实现[/quote] 那你就这样写:

template <typename Tout, typename Tin>
Tout Add(Tin x, Tin y)
{
    return x + y;
}
 
template <>
char* Add<char*, char>(char x, char y)
{
	char* p = new char[2];
	p[0] = x;
	p[1] = y;
    return p;
}
[/quote] 不行,你的这个连编译都通过不了。[/quote] 怎么不能通过了,你编译器太老了吧
g_style 2015-11-10
  • 打赏
  • 举报
回复
引用 10 楼 paschen的回复:
[quote=引用 9 楼 qq_27542855 的回复:] [quote=引用 8 楼 paschen的回复:][quote=引用 7 楼 qq_27542855 的回复:] [quote=引用 6 楼 iyomumx的回复:][quote=引用 5 楼 yshuise 的回复:] [quote=引用 2 楼 iyomumx 的回复:]
template <typename T1, typename T2>
auto Add(T1 x, T2 y)
{
    return x + y;
}

template <>
auto Add<char, char>(char x, char y)
{
    return std::string("") + x + y;
}
有点小问题,特化char,应该结果是char,而不是string[/quote] 按楼主的要求就是string啊,另外两个char相加应该是int[/quote] char类型的两个变量相加是一个字符串。 e.g char a = 'a', b='b'. 那么应该返回"ab".[/quote] 那你想把"ab"存哪? 动态分配一个char*指针来存?[/quote] 可以啊!就是用函数模板实现[/quote] 那你就这样写:

template <typename Tout, typename Tin>
Tout Add(Tin x, Tin y)
{
    return x + y;
}
 
template <>
char* Add<char*, char>(char x, char y)
{
	char* p = new char[2];
	p[0] = x;
	p[1] = y;
    return p;
}
[/quote] 不行,你的这个连编译都通过不了。
g_style 2015-10-17
  • 打赏
  • 举报
回复
引用 8 楼 paschen的回复:
[quote=引用 7 楼 qq_27542855 的回复:] [quote=引用 6 楼 iyomumx的回复:][quote=引用 5 楼 yshuise 的回复:] [quote=引用 2 楼 iyomumx 的回复:]
template <typename T1, typename T2>
auto Add(T1 x, T2 y)
{
    return x + y;
}

template <>
auto Add<char, char>(char x, char y)
{
    return std::string("") + x + y;
}
有点小问题,特化char,应该结果是char,而不是string[/quote] 按楼主的要求就是string啊,另外两个char相加应该是int[/quote] char类型的两个变量相加是一个字符串。 e.g char a = 'a', b='b'. 那么应该返回"ab".[/quote] 那你想把"ab"存哪? 动态分配一个char*指针来存?[/quote] 可以啊!就是用函数模板实现
paschen 版主 2015-10-17
  • 打赏
  • 举报
回复
引用 9 楼 qq_27542855 的回复:
[quote=引用 8 楼 paschen的回复:][quote=引用 7 楼 qq_27542855 的回复:] [quote=引用 6 楼 iyomumx的回复:][quote=引用 5 楼 yshuise 的回复:] [quote=引用 2 楼 iyomumx 的回复:]
template <typename T1, typename T2>
auto Add(T1 x, T2 y)
{
    return x + y;
}

template <>
auto Add<char, char>(char x, char y)
{
    return std::string("") + x + y;
}
有点小问题,特化char,应该结果是char,而不是string[/quote] 按楼主的要求就是string啊,另外两个char相加应该是int[/quote] char类型的两个变量相加是一个字符串。 e.g char a = 'a', b='b'. 那么应该返回"ab".[/quote] 那你想把"ab"存哪? 动态分配一个char*指针来存?[/quote] 可以啊!就是用函数模板实现[/quote] 那你就这样写:

template <typename Tout, typename Tin>
Tout Add(Tin x, Tin y)
{
    return x + y;
}
 
template <>
char* Add<char*, char>(char x, char y)
{
	char* p = new char[2];
	p[0] = x;
	p[1] = y;
    return p;
}
paschen 版主 2015-10-15
  • 打赏
  • 举报
回复
引用 7 楼 qq_27542855 的回复:
[quote=引用 6 楼 iyomumx的回复:][quote=引用 5 楼 yshuise 的回复:] [quote=引用 2 楼 iyomumx 的回复:]
template <typename T1, typename T2>
auto Add(T1 x, T2 y)
{
    return x + y;
}

template <>
auto Add<char, char>(char x, char y)
{
    return std::string("") + x + y;
}
有点小问题,特化char,应该结果是char,而不是string[/quote] 按楼主的要求就是string啊,另外两个char相加应该是int[/quote] char类型的两个变量相加是一个字符串。 e.g char a = 'a', b='b'. 那么应该返回"ab".[/quote] 那你想把"ab"存哪? 动态分配一个char*指针来存?
g_style 2015-10-15
  • 打赏
  • 举报
回复
引用 6 楼 iyomumx的回复:
[quote=引用 5 楼 yshuise 的回复:] [quote=引用 2 楼 iyomumx 的回复:]
template <typename T1, typename T2>
auto Add(T1 x, T2 y)
{
    return x + y;
}

template <>
auto Add<char, char>(char x, char y)
{
    return std::string("") + x + y;
}
有点小问题,特化char,应该结果是char,而不是string[/quote] 按楼主的要求就是string啊,另外两个char相加应该是int[/quote] char类型的两个变量相加是一个字符串。 e.g char a = 'a', b='b'. 那么应该返回"ab".
lm_whales 2015-10-14
  • 打赏
  • 举报
回复
char, string 都返回 string就可以了
先定义一个通用模板
再偏特化,或者特化即可
iyomumx 2015-10-14
  • 打赏
  • 举报
回复
引用 5 楼 yshuise 的回复:
[quote=引用 2 楼 iyomumx 的回复:]
template <typename T1, typename T2>
auto Add(T1 x, T2 y)
{
    return x + y;
}

template <>
auto Add<char, char>(char x, char y)
{
    return std::string("") + x + y;
}
有点小问题,特化char,应该结果是char,而不是string[/quote] 按楼主的要求就是string啊,另外两个char相加应该是int
yshuise 2015-10-14
  • 打赏
  • 举报
回复
引用 2 楼 iyomumx 的回复:
template <typename T1, typename T2>
auto Add(T1 x, T2 y)
{
    return x + y;
}

template <>
auto Add<char, char>(char x, char y)
{
    return std::string("") + x + y;
}
有点小问题,特化char,应该结果是char,而不是string
iyomumx 2015-10-14
  • 打赏
  • 举报
回复
引用 3 楼 qq_27542855 的回复:
[quote=引用 2 楼 iyomumx的回复:]
template <typename T1, typename T2>
auto Add(T1 x, T2 y)
{
    return x + y;
}

template <>
auto Add<char, char>(char x, char y)
{
    return std::string("") + x + y;
}
有问题,你的模板,可以实现一下吗[/quote] 有什么问题,你看编译都OK的: http://ideone.com/yoiHAw
g_style 2015-10-14
  • 打赏
  • 举报
回复
引用 2 楼 iyomumx的回复:
template <typename T1, typename T2>
auto Add(T1 x, T2 y)
{
return x + y;
}

template <>
auto Add<char, char>(char x, char y)
{
return std::string("") + x + y;
}
有问题,你的模板,可以实现一下吗
加载更多回复(1)

64,639

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧