专业化 C++基于类模板参数的成员函数

发布于 2024-11-19 20:08:40 字数 1269 浏览 1 评论 0原文

我有一个带有模板参数的类,它应该决定它包含两种类型的数据中的哪一种。基于该参数,我想通过两种不同的方式之一来实现成员函数。我尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜司空 2024-11-26 20:08:40

由于无论如何您都会创建两个不同版本的 getRange(),因此您始终可以根据 PadSide 的类型重载您的 struct String 成员函数代码>.我知道它并不那么“漂亮”,但最终,它仍然是类似数量的代码,并且您不必创建多个类类型。

template<PadSide Pad>
struct String
{
    void getRange();
};

template<>
void String<Right>::getRange() { /*....*/ }

template<>
void String<Left>::getRange() { /*....*/ }

Since you are going to be making two different versions of getRange() anyways, you can always overload your struct String member functions depending on the type of PadSide. 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.

template<PadSide Pad>
struct String
{
    void getRange();
};

template<>
void String<Right>::getRange() { /*....*/ }

template<>
void String<Left>::getRange() { /*....*/ }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文