C++ - 使用该方法的部分规范重载模板化类方法
堆栈溢出上已经有一些与此类似的问题,但似乎没有什么可以直接回答我的问题。如果我重新发布,我深表歉意。
我想用这些方法的部分模板特化来重载模板类的一些方法(带有 2 个模板参数)。我无法找出正确的语法,并且开始认为这是不可能的。我想我应该在这里发帖看看是否能得到确认。
示例代码如下:
template <typename T, typename U>
class Test
{
public:
void Set( T t, U u );
T m_T;
U m_U;
};
// Fully templated method that should be used most of the time
template <typename T, typename U>
inline void Test<T,U>::Set( T t, U u )
{
m_T=t;
m_U=u;
}
// Partial specialisation that should only be used when U is a float.
// This generates compile errors
template <typename T>
inline void Test<T,float>::Set( T t, float u )
{
m_T=t;
m_U=u+0.5f;
}
int _tmain(int argc, _TCHAR* argv[])
{
Test<int, int> testOne;
int a = 1;
testOne.Set( a, a );
Test<int, float> testTwo;
float f = 1.f;
testTwo.Set( a, f );
}
我知道我可以编写整个类的部分特化,但这有点糟糕。这样的事情可能吗?
(我用的是VS2008) 编辑:这是编译错误 错误 C2244:“Test::Set”:无法将函数定义与现有声明匹配
谢谢:)
There are a few questions already similar to this already on stack overflow, but nothing that seemd to directly answer the question I have. I do apologise if I am reposting.
I'd like to overload a few methods of a templated class (with 2 template parameters) with a partial template specialisation of those methods. I haven't been able to figure out the correct syntax, and am starting to think that it's not possible. I thought I'd post here to see if I can get confirmation.
Example code to follow:
template <typename T, typename U>
class Test
{
public:
void Set( T t, U u );
T m_T;
U m_U;
};
// Fully templated method that should be used most of the time
template <typename T, typename U>
inline void Test<T,U>::Set( T t, U u )
{
m_T=t;
m_U=u;
}
// Partial specialisation that should only be used when U is a float.
// This generates compile errors
template <typename T>
inline void Test<T,float>::Set( T t, float u )
{
m_T=t;
m_U=u+0.5f;
}
int _tmain(int argc, _TCHAR* argv[])
{
Test<int, int> testOne;
int a = 1;
testOne.Set( a, a );
Test<int, float> testTwo;
float f = 1.f;
testTwo.Set( a, f );
}
I know that I could write a partial specialisation of the entire class, but that kinda sucks. Is something like this possible?
(I'm using VS2008)
Edit: Here is the compile error
error C2244: 'Test::Set' : unable to match function definition to an existing declaration
Thanks :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果不定义类模板本身的部分特化,则无法部分特化成员函数。请注意,模板的部分特化仍然是模板,因此当编译器看到
Test
时,它期望类模板的部分特化。--
C++ 标准 (2003) 中的 $14.5.4.3/1 说,
然后标准本身给出了这个例子,
我希望标准中的引用和例子能够很好地回答你的问题。
You cannot partially specialize a member function without defining partial specialization of the class template itself. Note that partial specialization of a template is STILL a template, hence when the compiler sees
Test<T, float>
, it expects a partial specialization of the class template.--
$14.5.4.3/1 from the C++ Standard (2003) says,
Then the Standard itself gives this example,
I hope the quotation from the Standard along with the example answers your question well.
您所描绘的特定问题很简单:
然后从您的
Test::Set
实现中调用foo
。如果您想要完整的通用性,则类似地使用具有静态帮助器成员函数的帮助器类,并部分专门化该帮助器类。
干杯&呵呵,
The particular problem you're sketching is easy:
Then call
foo
from yourTest::Set
implementation.If you want the full generality, then similarly use a helper class with static helper member functions, and partially specialize that helper class.
Cheers & hth.,
如果您不想在代码中引入额外的函数、方法或类,那么还有另一种解决部分专业化问题的方法。
There's also another solution to the partial specialization problem, if you don't want to introduce additional functions, methods or classes to your code.