3

《C++ 编程语言》一书(第四版)。第 28.4 章(第 796 页)解释了 enable_if 并给出了使 operator->() 的定义成为条件的示例。书中的例子只是一个代码片段,我将其完成为一个程序如下:

#include <iostream>
#include <type_traits>
#include <complex>
using namespace std;
template<bool B, typename T>
using Enable_if=typename std::enable_if<B,T>::type;

template<typename T>
constexpr bool Is_class(){  //the book example misses constexpr
  return std::is_class<T>::value;
}

template<typename T>
class Smart_pointer{
public:
  Smart_pointer(T* p):data(p){
  }
  T& operator*();
  Enable_if<Is_class<T>(),T>* operator->(){
    return data;
  }
  ~Smart_pointer(){
    delete data;
  }
private:
  T* data;  
};
int main()
{
  Smart_pointer<double> p(new double);  //compiling error in g++ 4.7.2: no type named 'type' in 'struct std::enable_if<false, double>'
  //Smart_pointer<std::complex<double>> q(new std::complex<double>);//compile successfully.
}  

上面的代码不能在 gcc 4.7.2 中编译。编译器抱怨:错误:'struct std::enable_if' 中没有名为 'type' 的类型

根据书中的解释,如果 T 不是类,则 operator->() 将被忽略。但是,这并不能解释编译错误。编译错误表明相反,即使 T 不是类,也不会忽略 operator->() 的定义。这篇文章 std::enable_if 似乎解释了编译错误,以有条件地编译成员函数。但该帖子似乎与书中的解释不一致。任何人都可以帮助解释成员函数的 enable_if 的用法吗?在上面的例子中,如果我只想在 T 是类时定义 operator->(),有没有一个干净优雅的解决方案?谢谢你。

非常感谢您的回复。还有另一种基于重载的解决方法(我不会说它比其他发布的解决方案更好)

template<typename T>
T* Smart_pointer<T>::operator->(){
  op_arrow(std::integral_constant<bool,Is_class<T>()>());
}
private: 
T* op_arrow(std::true_type){
return data; 
}
T* op_arrow(std::false_type);
4

2 回答 2

3

首先,正如@jrok 和您链接到的帖子所提到的:您必须有一个模板函数才能使用enable_if. 但在您的特定情况下这不是一个好主意,因为没有理由这样做operator->模板化。而且,它不起作用!因为没有办法实例化特定的 operator->()!它没有(也不能)任何参数,并且当您在某个对象上调用它时没有语法来指定它!所以,T(或任何虚拟类型)将/不能从这个调用中推断出来。

因此,作为一种解决方法,您可以使用编译时条件继承。即这样的:

template <typename T, bool IsClass>
struct smart_base
{
    struct base {};
};

template <typename T>
struct smart_base<T, true>
{
    struct base 
    {
        T* operator->()
        {
            // do that ever you wanted to do
        }
    };
};

template <typename T>
struct smart : public smart_base<T, std::is_class<T>::value>::base
{
    // depending on T here you have, or have no operator-> inherited
};

您必须了解,operator->实际上在您的基类中需要将一些函数和/或数据成员移动到基类的基类:) 或者您可以使用 CRTP 技术从基类访问派生类的成员一 :)

于 2013-08-06T19:53:58.207 回答
1

您可以在 C++11 中进行约束operator->,但这需要一个相当尴尬的技巧。您必须使成员成为模板,并且必须使Enable_if依赖于模板参数,以便在实例化成员时发生 SFINAE,而不是在实例化类时发生简单错误(Live at Coliru):

template<typename T>
class Smart_pointer {
public:
  ...
  template <class U=T,class=Enable_if<Is_class<U>(),void>>
  T* operator->() const { return data; }
};

也就是说,我不知道这样做是否真的是个好主意。与不适当的 一起使用时的错误消息T

main.cpp:35:4: error: base operand of ‘->’ has non-pointer type ‘Smart_pointer<double>’
   p->foo();
    ^

如果您只是无条件地声明,那么您得到operator->的并没有太大改善:

template<typename T>
class Smart_pointer {
public:
  ...
  T* operator->() const { return data; }
};

main.cpp:35:6: error: request for member ‘foo’ in ‘* p.Smart_pointer<T>::operator-><double>()’, which is of non-class type ‘double’
   p->foo();
      ^

并且每个读过您的代码的人都不必要求您解释到底发生了什么operator->

于 2013-08-06T20:33:25.037 回答