【问题标题】:Class template argument deduction of member class template成员类模板的类模板参数推导
【发布时间】:2019-02-28 10:02:31
【问题描述】:

考虑以下

template <typename T1>
struct OuterFoo {
  template <typename T2>
  struct InnerFoo  {
    InnerFoo(T2 v2) {}
  };

  explicit OuterFoo(T1 v1) {}
};

int main() {
  OuterFoo outer_foo{6};
  OuterFoo<int>::InnerFoo inner_foo{3};

  return 0;
}

https://godbolt.org/z/RjUuWM

clang 拒绝编译它,声明“没有可行的构造函数或推导指南来推导 'InnerFoo' 的模板参数” - 另一方面,Gcc 编译没有问题。

所提供的示例是否正确(按照标准)CTAD 的用法并且这里有问题?

如果提供了推导指南,clang 编译正常,但 gcc 不接受该指南:https://godbolt.org/z/4b-9Cr

template <typename T1>
struct OuterFoo {
  template <typename T2>
  struct InnerFoo  {
    InnerFoo(T2 v2) {}
  };

  // This Template Deduction Guide is required in clang 6
  // for the example to compile.
  // GCC compiles without this TDG but reports an error
  // with it
  // "deduction guide ‘OuterFoo<T1>::InnerFoo(T2) -> OuterFoo<T1>::InnerFoo<T2>’
  // must be declared at namespace scope"
  template<typename T2> InnerFoo(T2 v2) -> InnerFoo<T2>;

  // ...but no TDG is required for OuterFoo
  explicit OuterFoo(T1 v1) {}
};

int main() {
  OuterFoo outer_foo{6};
  OuterFoo<int>::InnerFoo inner_foo{3};

  return 0;
}

clang 和 gcc 中成员模板类的 CTAD 状态如何?是不是还没有?

【问题讨论】:

  • 一个随机的问题,为什么人们仍然使用模板而不是auto

标签: c++ templates c++17


【解决方案1】:

clang 和 gcc 中成员模板类的 CTAD 状态如何?是不是还没有?

根据language features support page for GCC,CTAD 可用于版本 7 及更高版本。

根据language features support page for Clang,CTAD 可用于版本 7 及更高版本。

但是,请注意,在这些实施的后续版本中报告并解决了一些缺陷。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多