【发布时间】: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;
}
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?