模板<>混乱 [英] Template<> confusion

查看:62
本文介绍了模板<>混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下事项:


class xyz

{

public:

template< ; typename T>

void foo(T x)

{

cout<< " FOO< T> " << x<< endl;

}


模板<> //删除这一行?

void foo(int x)

{

cout<< " FOO< INT> " << x<<结束;

}

};


此代码在MS VS C ++ 2003和Intel C ++ 9上编译,但不在Dev-上编译C ++

4.9.9.2。但是,如果我注释掉template<>这一行,它会在所有3个编译器上编译

罚款。


有人可以解释一下关于此事是标准的C ++吗?

谢谢。


克里斯

Consider the following:

class xyz
{
public:
template <typename T>
void foo(T x)
{
cout << "foo<T> " << x << endl;
}

template <> // remove this line or not?
void foo(int x)
{
cout << "foo<int> " << x << endl;
}
};

This code compiles on MS VS C++ 2003 and Intel C++ 9 but not on Dev-C++
4.9.9.2. However, if I comment out the line "template<>", it compiles
fine on all 3 compilers.

Can someone please explain what is the standard C++ on this matter?
Thank you.

Chris

推荐答案



" SoilMan" < CB **** @ yahoo.com>在消息中写道

news:11 ********************** @ o13g2000cwo.googlegr oups.com ...

"SoilMan" <cb****@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
请考虑以下内容:

类xyz
{
公开:
模板< typename T>
void foo(T x)
{
cout<< " FOO< T> " << x<<结束;
}

将以下内容移出此范围:template<> //删除这一行?
void foo(int x)
{
cout<< " FOO< INT> " << x<<结束;
}
};
Consider the following:

class xyz
{
public:
template <typename T>
void foo(T x)
{
cout << "foo<T> " << x << endl;
}

Move the following out of this scope: template <> // remove this line or not?
void foo(int x)
{
cout << "foo<int> " << x << endl;
}
};




并将其放在这里:


template<> ;

void xyz :: foo(int x)

{

cout<< " FOO< INT> " << x<<结束;

}


问候,

Sumit。

-

Sumit Rajan< su **** @ msdc.hcltech.com>



and place it here:

template <>
void xyz::foo(int x)
{
cout << "foo<int> " << x << endl;
}

Regards,
Sumit.
--
Sumit Rajan <su****@msdc.hcltech.com>


无需放置模板<>当你定义一个特定的

实现时。

There is no need to place template <> when u defining a specific
implementation.




SoilMan写道:

SoilMan wrote:

类xyz
{
公开:
模板< typename T>
void foo(T x)
{
cout<< " FOO< T> " << x<< endl;
}

模板<> //删除这一行?
void foo(int x)
{
cout<< " FOO< INT> " << x<< endl;
}
};

此代码在MS VS C ++ 2003和Intel C ++ 9上编译,但不在Dev-C ++上编译.4.9.9.2。但是,如果我注释掉template<>这一行,它会在所有3个编译器上编译好。

有人可以解释一下这个问题的标准C ++是什么吗?
Consider the following:

class xyz
{
public:
template <typename T>
void foo(T x)
{
cout << "foo<T> " << x << endl;
}

template <> // remove this line or not?
void foo(int x)
{
cout << "foo<int> " << x << endl;
}
};

This code compiles on MS VS C++ 2003 and Intel C++ 9 but not on Dev-C++
4.9.9.2. However, if I comment out the line "template<>", it compiles
fine on all 3 compilers.

Can someone please explain what is the standard C++ on this matter?




会员模板的专业化只能在

类定义中声明。


无论如何,你不需要专业化,功能重载就足够了。



A specialization of member template can only be declared out of the
class definition.

Anyway, you don''t need specialization here, function overloading would
suffice.


这篇关于模板&LT;&GT;混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆