[英]Template class Member function specialization
Before someone has marked this question as duplicate of this ,please let me explain my problem. 有人将其标记这个问题进行重复之前此 ,请让我解释一下我的问题。 I have a class template with member functions which are not templates.I want to have additional version of the body of such functions for a specific class template param.In my case it can be only two variations: "true" or "false"
我有一个类模板,其成员函数不是模板。我想为特定的类模板参数提供此类函数主体的附加版本。在我的情况下,它只能是两个变体:“ true”或“ false”
Example: 例:
template <bool IsEven>
class EvenOddProcessor{
public:
bool ProcessEvenOdd();
};
//Default definition:
template<bool IsEven>
inline bool EvenOddProcessor<IsEven>::ProcessEvenOdd()
{
return false;
}
//Now I want to define the special case of the function
//when the class template param is true:
inline bool EvenOddProcessor<true>::ProcessEvenOdd()
{
return true;
}
Now,this works with MSVC compilers and doesn't with GCC.This is unsurprising because GCC is always more strict about C++ standard,which as far as I understand,doesn't allow member function specialization.However based on the answers to this question what I am trying to do still should be possible with some template magic.None of the solutions in that answers worked in my case. 现在,这适用于MSVC编译器,不适用于GCC。这不足为奇,因为GCC始终对C ++标准更加严格,据我所知,它不允许成员函数专门化。但是,基于此问题的答案某些模板魔术仍然可以实现我正在尝试做的事情。
You just need to mark it as a specialisation with template <>
: 您只需要使用
template <>
将其标记为专业化:
template <>
inline bool EvenOddProcessor<true>::ProcessEvenOdd()
{
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.