有关函数模板的问题

lw1a2 2008-10-24 08:27:05
#include <iostream>
using namespace std;

class B
{
public:
template <class T>
void f(T t)
{
cout << t << endl;
}

//这里为什么不加这个声明也行?
//template <>
//void f<double>(double d);
};

//类里不声明也能特化?
template <>
void B::f<double>(double d)
{
cout << "double" << endl;
}

int main()
{
B b;
int i = 0;
b.f(i);

double d = 1.0;
b.f(d);

return 0;
}


编译器VC2003
为什么类里不用特化声明也可以?
是我的编译器特殊?还是C++标准里有定义?书上举得例子都是有声明的。



...全文
216 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
rularys 2008-10-25
  • 打赏
  • 举报
回复
不熟悉,学习
baihacker 2008-10-24
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 lw1a2 的回复:]
没有显示特化?
[/Quote]
在{}里面的时候不认为是一个namespace.
而一个class是一个namespace,也是标准上说的.
对于这个问题,我在标准上没有找到答案.

只能认为在{}不认为已经引入一个namespace.
但是已经引入一个class type.

猜测:
标准中对模板嵌套的模板有规定,可能和这个有关.
lw1a2 2008-10-24
  • 打赏
  • 举报
回复
没有显示特化?
baihacker 2008-10-24
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 lw1a2 的回复:]
引用 15 楼 baihacker 的回复:
我就是说去掉注释后,g++拒绝这种做法.

你是说在GCC下,这段编译不过?

C/C++ code#include <iostream>
using namespace std;

class B
{
public:
template <class T>
void f(T t)
{
cout << t << endl;
}

//这里的声明还是有点用的
template <>
void f<double>(double d);
};

int main()
{
B b;
int i = 0;
b.f…
[/Quote]
我在2楼就给出了信息了.
lw1a2 2008-10-24
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 baihacker 的回复:]
我就是说去掉注释后,g++拒绝这种做法.
[/Quote]
你是说在GCC下,这段编译不过?
#include <iostream>
using namespace std;

class B
{
public:
template <class T>
void f(T t)
{
cout << t << endl;
}

//这里的声明还是有点用的
template <>
void f<double>(double d);
};

int main()
{
B b;
int i = 0;
b.f(i);

double d = 1.0;
b.f(d);

return 0;
}

template <>
void B::f<double>(double d)
{
cout << "double" << endl;
}

weiyijiji 2008-10-24
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 lw1a2 的回复:]
C/C++ code#include <iostream>
using namespace std;

class B
{
public:
template <class T>
void f(T t)
{
cout << t << endl;
}

//这里的声明还是有点用的
//template <>
//void f<double>(double d);
};

int main()
{
B b;
int i = 0;
b.f(i);

double d = 1.0;
b.f(d);

return 0;
}

template <>
void B::f<double>(double…
[/Quote]
这个VS2005会报错,意思是实例化后不能在进行特化.这里会出现2个重复函数定义.
类中是否声明在VS2005中都正确.既然能在类的作用域中找到普通版本,应该就不需要声明,就象在全局作用域一样
baihacker 2008-10-24
  • 打赏
  • 举报
回复
我就是说去掉注释后,g++拒绝这种做法.
lw1a2 2008-10-24
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 baihacker 的回复:]
C/C++ code//这里的声明还是有点用的
//template <>
//void f<double>(double d);


我这里被g++拒绝掉这种声明.
[/Quote]
把注释去掉呢
#include <iostream>
using namespace std;

class B
{
public:
template <class T>
void f(T t)
{
cout << t << endl;
}

//这里的声明还是有点用的
template <>
void f<double>(double d);
};

int main()
{
B b;
int i = 0;
b.f(i);

double d = 1.0;
b.f(d);

return 0;
}

template <>
void B::f<double>(double d)
{
cout << "double" << endl;
}
baihacker 2008-10-24
  • 打赏
  • 举报
回复
//这里的声明还是有点用的
//template <>
//void f<double>(double d);

我这里被g++拒绝掉这种声明.
lw1a2 2008-10-24
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;

class B
{
public:
template <class T>
void f(T t)
{
cout << t << endl;
}

//这里的声明还是有点用的
//template <>
//void f<double>(double d);
};

int main()
{
B b;
int i = 0;
b.f(i);

double d = 1.0;
b.f(d);

return 0;
}

template <>
void B::f<double>(double d)
{
cout << "double" << endl;
}
yshuise 2008-10-24
  • 打赏
  • 举报
回复
template <>
void
B::f<double>(double d)
{
cout < < "double" < < endl;
}
yshuise 2008-10-24
  • 打赏
  • 举报
回复
template <>
void
B::f
<double>(double d)
{
cout << "double" << endl;
}
这儿指定了作用域,同在类中特化有什么区别?这是很基础的知识。
lw1a2 2008-10-24
  • 打赏
  • 举报
回复
不知道Solaris的CC上能不能编过,现在没法试
baihacker 2008-10-24
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 akirya 的回复:]
g++ 4.2.1编译通过.
[/Quote]
别人问的就是为什么能通过...

在B scope内有primer template
然后在外面对B scope内的primer template进行specialize当然可以.

但是,直接在{}内进行specialize声明是不行的.

g++的报错认为在{}不算真正的B scope.
{}后才引入了一个B scope.
lw1a2 2008-10-24
  • 打赏
  • 举报
回复
也就是说这种写法是不标准的?
  • 打赏
  • 举报
回复
g++ 4.2.1编译通过.
yshuise 2008-10-24
  • 打赏
  • 举报
回复
主模板可以只有声明无需定义。
devil_zuiai 2008-10-24
  • 打赏
  • 举报
回复
up
yshuise 2008-10-24
  • 打赏
  • 举报
回复
特化是对主模板的特化,不是对什么定义问题。
baihacker 2008-10-24
  • 打赏
  • 举报
回复
//template <>
//void f<double>(double d);

g++直接拒绝了这种做法:
test.cpp:14: error: invalid explicit specialization before '>' token
test.cpp:14: error: explicit specialization in non-namespace scope `class B'

65,211

社区成员

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

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