【问题标题】:Non deducible context in template region模板区域中的不可推断上下文
【发布时间】:2011-12-06 09:31:10
【问题描述】:
template<typename T>
struct Wrap {
  Wrap(T *p) {}
};

#ifdef TEMPLATE
template<typename T>
void foo (Wrap<T> t) {}  // version-1

#else
void foo (Wrap<int> p) {} // version-2
#endif

int main () {
  foo(new int);
}

编译#else 部分时,编译正常并选择了version-2。如果我尝试编译 #ifdef 部分,我希望应该选择 version-1。但是编译器给出了错误,

错误:没有匹配的函数调用 `foo(int*)'

我是否触及了template foo 的不可演绎部分?如果是,那么任何人都可以澄清 non-deducible 区域的确切规则是什么?

【问题讨论】:

    标签: c++ templates wrapper


    【解决方案1】:

    类模板的类型不能通过传递给其构造函数的参数来确定。要知道哪些构造函数可用,编译器必须已经选择了要实例化的Wrap

    #else 块中,您已明确选择实例化Wrap&lt;int&gt;,因此编译器知道使用隐式Wrap&lt;int&gt;(int*) 构造函数。


    也许非正式地:一个类型是不可推导的,如果推导它的前提是知道要推导的类型是什么。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-05
      相关资源
      最近更新 更多