C++ 有一个有用的功能,即模板参数隐含在模板类内的代码中A。然而,对于建筑来说,这似乎与 CTAD 发生冲突。
如何让 CTAD 优先?
例如,在这里,成员中有一个错误f,因为A它被解释为A<T>where Tis std::string,而不是从参数 推导出来double。
#include<string>
template<class T>
struct A {
A(T const& t) {}
auto f() {return A{5.0};} // error here triggered by line 11 `auto a2 = a1.f();`
};
int main() {
A a1{std::string{"hello"}};
auto a2 = a1.f();
}
Run Code Online (Sandbox Code Playgroud)