相关疑难解决方法(0)

仅接受某些类型的C++模板

在Java中,您可以定义通用类,它只接受扩展您选择的类的类型,例如:

public class ObservableList<T extends List> {
  ...
}
Run Code Online (Sandbox Code Playgroud)

这是使用"extends"关键字完成的.

在C++中是否有一些简单的等效关键字?

c++ templates

144
推荐指数
11
解决办法
10万
查看次数

`is_base_of`是如何工作的?

以下代码如何工作?

typedef char (&yes)[1];
typedef char (&no)[2];

template <typename B, typename D>
struct Host
{
  operator B*() const;
  operator D*();
};

template <typename B, typename D>
struct is_base_of
{
  template <typename T> 
  static yes check(D*, T);
  static no check(B*, int);

  static const bool value = sizeof(check(Host<B,D>(), int())) == sizeof(yes);
};

//Test sample
class Base {};
class Derived : private Base {};

//Expression is true.
int test[is_base_of<Base,Derived>::value && !is_base_of<Derived,Base>::value];
Run Code Online (Sandbox Code Playgroud)
  1. 请注意,这B是私人基地.这是如何运作的?

  2. 注意operator B*()是const.它为什么如此重要?

  3. 为什么template<typename T> static yes …

c++ templates overloading type-traits implicit-conversion

116
推荐指数
3
解决办法
2万
查看次数