相关疑难解决方法(0)

模板参数推导和 cons 限定

谁能解释一下为什么代码不能编译。

template<class T, class DER>
struct Base {
  T a;
  Base(const T argB) : a(argB){}
};

template<class T>
struct Derived : Base<T, Derived<T> > {
  Derived(const T argD) : Base<T, Derived<T> >(argD){}
};

int main() {
  int val = 10;
  const int *p = &val;
  /* this was in the original question
  Derived<int*> d(p); // breaks, but compiles with Derived<const int*> d(p);
  */
  Derived d(p); // fails, but Derived<const int*> d(p); compiles
}
Run Code Online (Sandbox Code Playgroud)

错误消息是关于没有从int*到 的转换const int*。正如我所看到的,它 …

c++ templates

5
推荐指数
2
解决办法
1266
查看次数

标签 统计

c++ ×1

templates ×1