考虑下面的代码:推导引导和可变参数模板
#include <tuple>
#include <iostream>
template <class T>
struct custom_wrapper
{
template <class Arg>
custom_wrapper(Arg arg): data(arg) {}
T data;
};
template <class Arg>
custom_wrapper(Arg arg) -> custom_wrapper<Arg>;
template <class... T>
struct custom_tuple
{
template <class... Args>
custom_tuple(Args... args): data(args...) {}
std::tuple<T...> data;
};
template <class... Args>
custom_tuple(Args... args) -> custom_tuple<Args...>;
int main(int argc, char* argv[])
{
custom_wrapper<int> w1(42); // OK
custom_wrapper w2(42); // OK
custom_tuple<int> t1(42); // OK
custom_tuple t2(42); // Fails
return 0;
}
失败下克++ 7返回以下错误的行:
variadic_deduction_guide.cpp: In instantiation of 'custom_tuple<T>::custom_tuple(Args ...) [with Args = {int}; T = {}]':
variadic_deduction_guide.cpp:31:23: required from here
variadic_deduction_guide.cpp:19:45: error: no matching function for call to 'std::tuple<>::tuple(int&)'
custom_tuple(Args... args): data(args...) {}
是正常或是编译器故障?
嗯,海湾合作委员会和铿锵树干不同意OP的代码,我不是很确信这里。首先是'T'“尾随”? (这是一个相当不明确的术语。)其次,你能证明构造函数比扣除指南更专业吗? –
@ T.C。但很酷,看到克朗与扣除指南工作!这一定是相当新的。 – Barry
这里的上下文是一个函数调用,所以[“所使用的类型\ [对于偏序} \是那些函数调用具有参数的函数参数类型]](https://timsong-cpp.github.io/cppwp /temp.deduct.partial#3.1)。对于部分排序目的,[“一个模板参数可能保留没有值,只要它不用于用于部分排序的类型”](https://timsong-cpp.github.io/cppwp/temp.deduct。部分#12),而'T ...'不是。那么究竟如何比其他更专业?扣除失败的方向如何? –