模板空括号初始化扣

Template empty bracket initialization deduction

我认为模板函数可以有默认参数参数(不是模板参数而是运行时参数)。我们还可以用空括号初始化来初始化 class。但是编译器是如何匹配模板的呢?

为什么这段代码可以编译,编译器是如何推导的,这个函数调用示例中的Args是什么?

我的理解: 默认括号初始化调用空构造函数,隐式创建,因为没有用户定义的构造函数或用户定义的默认构造函数。也就是说,我们可以用 {} 初始化任何包。所以推论不适用于那里,因为我们不能选择一个包,每个包都是候选包。也许默认的可变参数模板参数是 <>(无参数)。

    template<typename...> class pack {};

    template<class... Args>
    inline auto make(pack<Args...> = {}) {

    }

    int main() { make(); }

(使用 GCC 编译) 注意:我认为不是,但默认参数可能有用:调用函数的 2 种方法:make < int, char, int >() (正常使用)或 make(myPack) 用于打包可变参数。

给定make();,推导的Args为空;在这种情况下,make();make<>(); 具有相同的效果。

模板参数是parameter pack, and no template arguments are provided here. Note that function default arguments don't participate in template argument deduction。然后Args被推导为空。

If a parameter pack appears as the last P, then the type P is matched against the type A of each remaining argument of the call. Each match deduces the template arguments for the next position in the pack expansion:

Type template parameter cannot be deduced from the type of a function default argument: