专业化 C++基于类模板参数的成员函数
我有一个带有模板参数的类,它应该决定它包含两种类型的数据中的哪一种。基于该参数,我想通过两种不同的方式之一来实现成员函数。我尝试使用 Boost Enable-If,但没有成功。这是我最惊讶的代码版本不起作用:
#include <boost/utility/enable_if.hpp>
enum PadSide { Left, Right };
template <int> struct dummy { dummy(int) {} };
template <PadSide Pad>
struct String
{
typename boost::enable_if_c<Pad == Left, void>::type
getRange(dummy<0> = 0) {}
typename boost::enable_if_c<Pad == Right, void>::type
getRange(dummy<1> = 0) {}
};
int main()
{
String<Left> field;
field.getRange();
}
对此,g++ 4.6.0 说:
no type named ‘type’ in ‘struct boost::enable_if_c<false, void>’
当然,第二个重载应该不起作用,但由于 SFINAE,它应该被忽略。如果我删除虚拟函数参数,g++ 会这样说:
‘typename boost::enable_if_c<(Pad == Right), void>::type
String<Pad>::getRange()‘
cannot be overloaded with
‘typename boost::enable_if_c<(Pad == Left), void>::type
String<Pad>::getRange()‘
这就是为什么我首先将虚拟参数放在那里的原因 - 按照 文档。
基本上我想要的是有两种 getRange() 实现,并根据 Pad 类型选择其中之一。我希望 Enable-If 能让我做到这一点,而无需创建辅助类来将工作委托给(我将同时尝试)。
I have a class with a template parameter which should decide which of two styles of data it contains. Based on that parameter I want to implement a member function one of two different ways. I tried using Boost Enable-If, but without success. Here's the version of the code that I'm most surprised doesn't work:
#include <boost/utility/enable_if.hpp>
enum PadSide { Left, Right };
template <int> struct dummy { dummy(int) {} };
template <PadSide Pad>
struct String
{
typename boost::enable_if_c<Pad == Left, void>::type
getRange(dummy<0> = 0) {}
typename boost::enable_if_c<Pad == Right, void>::type
getRange(dummy<1> = 0) {}
};
int main()
{
String<Left> field;
field.getRange();
}
To this, g++ 4.6.0 says:
no type named ‘type’ in ‘struct boost::enable_if_c<false, void>’
Of course, the second overload is supposed to not work, but it's supposed to be ignored due to SFINAE. If I remove the dummy function parameters, g++ says this:
‘typename boost::enable_if_c<(Pad == Right), void>::type
String<Pad>::getRange()‘
cannot be overloaded with
‘typename boost::enable_if_c<(Pad == Left), void>::type
String<Pad>::getRange()‘
Which is why I put the dummy parameters there in the first place--following the Compiler Workarounds section of the documentation.
Basically what I want is to have two implementations of getRange(), and have one or the other be selected based on the Pad type. I was hoping that Enable-If would let me do it without making auxiliary classes to delegate the work to (which I'm going to try in the meantime).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于无论如何您都会创建两个不同版本的
getRange()
,因此您始终可以根据PadSide
的类型重载您的struct String
成员函数代码>.我知道它并不那么“漂亮”,但最终,它仍然是类似数量的代码,并且您不必创建多个类类型。Since you are going to be making two different versions of
getRange()
anyways, you can always overload yourstruct String
member functions depending on the type ofPadSide
. I know it's not as "pretty", but in the end, it's still a similar amount of code, and you won't have to make multiple class types.