g++对模板特化的支持怎样?我的程序为什么在g++上编译不过呢?

mdj_boy 2006-02-09 05:37:39
我使用的是redhat linux 9自带的g++,代码如下:

//GNUCTest.cpp
#include <iostream>

class Util
{
public:
template<>
int func(int arg)
{
return arg+1;
}
};

int main (int argc, char *argv[])
{
Util util;
int arg = util.func(12);

std::cout << "hello world" << std::endl;
std::cout << arg << std::endl;

return 0;
}

vc6下没有问题.

在g++下:
g++ -c -ansi -traditional -x c++ -O2 -g -o "Debug/GNUCTest.o" "GNUCTest.cpp"
GNUCTest.cpp:7: error: invalid explicit specialization before '>' token
GNUCTest.cpp:7: error: explicit specialization in non-namespace scope `class Util'
GNUCTest.cpp:9: error: invalid member function declaration
GNUCTest.cpp: In function `int main(int, char**)':
GNUCTest.cpp:19: error: 'class Util' has no member named 'func2'
make: *** [Debug/GNUCTest.o] Error 1

那位高人能不吝赐教!3Q~
...全文
282 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
ddddh 2006-02-11
  • 打赏
  • 举报
回复
对于编译器来说,符合标准才叫强吧?:-)
cryincold 2006-02-11
  • 打赏
  • 举报
回复
是啊.vc7很接近c++标准了
shark0001 2006-02-10
  • 打赏
  • 举报
回复
g++对模板化的支持是没问题的,不说是最好,也是非常好的了.

mdj_boy 2006-02-10
  • 打赏
  • 举报
回复
to:vollin(林尚义)
谢谢回复,但有两个问题:

1、函数模板特化应该写成这样吧:

template<>
void func(int a);

2、函数模板是不支持偏特化的
mdj_boy 2006-02-10
  • 打赏
  • 举报
回复
试验了一下,看来还是VC7强啊~~~
两种写法都支持.
ddddh 2006-02-10
  • 打赏
  • 举报
回复
从C++标准14.7.3里面摘录出来的:

"An explicit specialization shall be declared in the namespace of which the template is a member, or, for member templates, in the namespace of which the enclosing class or enclosing class template is a member. An explicit specialization of a member function, member class or static data member of a class template

**注意下面这句话**shall be declared in the namespace of which the class template is a member.

Such a declaration may also be a definition. If the declaration is not a definition, the specialization may be defined later in the namespace in which the explicit specialization was declared, or in a namespace that encloses the one in which the explicit specialization was declared."

也就是说,需要在"包含它的那个类所属的名字空间"里面定义特化的成员函数、嵌套的类,以及静态的数据成员。


我想应该是这个意思了:-)
mdj_boy 2006-02-10
  • 打赏
  • 举报
回复
ddddh(叶君临) 的代码好用~~~3Q!

但这种写法挺怪的~~~不需要在class foo中声明?!
mdj_boy 2006-02-10
  • 打赏
  • 举报
回复
又见甘草~~~
甘草兄能否指条明路,g++下的函数模板特化怎么写?
ddddh 2006-02-10
  • 打赏
  • 举报
回复
// 下面的代码在gcc 3.4.2上面能工作:-)


#include <iostream>

class foo
{
public:
template <typename T>
void bar(T t)
{
std::cout << t << std::endl;
}

};

template <>
void foo::bar(int i)
{
std::cout << "int = " << i << std::endl;
}


int main()
{
foo f;
f.bar(1);
f.bar("111");

return 0;
}
healer_kx 2006-02-10
  • 打赏
  • 举报
回复
看介绍Boost那本书,G++是个鸟~VC7对模板的支持还是相当好的.

函数模板是不支持偏特化的
,对!
vollin 2006-02-09
  • 打赏
  • 举报
回复
更正一下,所有的编译器的特化都是没有问题的,
但是在偏特化时V6非常差。

特化->
template<class T>
void func(T a);

其一个特化
void func(int a);

其一个偏特化为
template<class T>
void func(vector<T> a);
vollin 2006-02-09
  • 打赏
  • 举报
回复
V6的模板特化是最差的。
G++的模板特化是比较好的,不过你使用模板的方式有点问题。

应该是先有一个模板,才有特化,偏特化说的法,你的写法是根本没用到模板,V6不报错只是怕
是一个bug.

64,682

社区成员

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

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