我可以使用的boost :: enable_if上的一个成员函数? [英] Can I use boost::enable_if on a member function?

查看:124
本文介绍了我可以使用的boost :: enable_if上的一个成员函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个模板类,我想允许的另一种方法,只存在于一定的模板类型。目前该方法存在于所有模板类型,但是会导致对所有其他类型的编译错误。

I'm writing a template class, and I want to allow an additional method to exist only for a certain template type. Currently the method exists for all template types, but causes a compilation error for all other types.

这更为复杂的是,它是一个重载运算符()。不知道是我想要做的实际上是可能在这里。

Complicating this is that it's an overloaded operator(). Not sure if what I want to do is actually possible here.

这是我现在有:

template<typename T, typename BASE>
class MyClass  : public BASE
{
public:

    typename T& operator() (const Utility1<BASE>& foo);
    typename T const& operator() (const Utility2<BASE>& foo) const;
};

我要的 T&安培; 的版本总是可用的,但 T const的&安培; 版本只适用公用程序&LT;碱基GT; 是有效的。眼下,这两种方法存在,但尝试使用const版本给人如果一个奇怪的编译错误公用程序&LT;碱基GT; 是无效的。我宁愿有一个有意义的错误,甚至出现没有这样的成员函数的错误。

I want the T& version always available, but the T const& version only available if Utility2<BASE> is valid. Right now, both methods exist, but attempting to use the const version gives a weird compilation error if Utility2<BASE> is invalid. I'd rather have a sensible error, or even a "no such member function" error.

这可能吗?

修改:通过升压文档阅读后,这里是什么我已经出来了,它似乎工作:

EDIT: After reading through the boost docs, here's what I've come up with, and it seems to work:

template<typename T, typename BASE>
class MyClass  : public BASE
{
public:

    typename T& operator() (const Utility1<BASE>& foo);

    template<typename U>
    typename boost::enable_if<boost::is_same<Utility2<BASE>, U>, T>::type const &
    operator() (const U& foo) const;
};

这样的方法并不存在,除非有人试图与公用程序使用它,如果它是有效的为基本类型,他们只能创建一个公用程序。但是,当它不是有效的为基本类型,MyClass的不会浪费时间创建访问方法。

So that method doesn't exist unless someone tries to use it with Utility2, and they can only create a Utility2 if it's valid for that BASE type. But when it's not valid for that BASE type, MyClass will not waste time creating the accessor method.

推荐答案

是的,这是可能的,但不直接与类模板参数。 的boost :: enable_if 只能用在方法本身的模板参数一起使用。所以,用一个小的typedef用法:

Yes, this is possible, but not with the class template parameter directly. boost::enable_if can only be used with a template parameter on the method itself. So, with a little typedef usage:

template<typename T, typename BASE>
class MyClass  : public BASE
{
public:
  typedef Utility2<BASE> util;

  typename T& operator() (const Utility1<BASE>& foo);

  template<typename U>
  typename boost::enable_if<boost::is_same<util, U>, T>::type const &
  operator() (const U& foo) const;
};

这工作,因为公用程序只能从某一个基类来创建。因此,如果基本类型是别的东西,const版本运营商()将不存在。

This works, because Utility2 can only be created from a certain BASE type. So if the BASE type is something else, the const version of operator() won't exist.

所以,这是一个非常次要的事情。它没有得到我很多。但它是整齐的事情。

So, it's a very minor thing. It doesn't gain me much. But it was neat to do.

这篇关于我可以使用的boost :: enable_if上的一个成员函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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