template-argument-deduction

Why must we specify template arguments for template base class when calling its constructor?

风流意气都作罢 提交于 2020-06-27 09:03:50
问题 The following code snippet compiles only if I explicitly specify the template argument T for the Base struct in Derived ctor: template <class T> struct Base { Base(int) {} }; template <class T> struct Derived : Base<T> { Derived(int i) : Base<T>(i) {} }; If I call Base(i) instead of Base<T>(i) - it doesn't work. Why can't compiler determine that Base is actually Base<T> (because I derive from Base<T> )? Is this requirement made intentionally? 来源: https://stackoverflow.com/questions/52632748

Why can't unique_ptr's template arguments be deduced?

我的未来我决定 提交于 2020-06-21 17:09:47
问题 When you have class template argument deduction available from C++17, why can't you deduce the template arguments of std::unique_ptr? For example, this gives me an error: std::unique_ptr smp(new D); That says "Argument list of class template is missing". Shouldn't the template arguments (at least the pointer type) be deducable? See this: any declaration that specifies initialization of a variable and variable template 回答1: I'm not going to repeat the rationale in @NathanOliver's great answer,

Why can't unique_ptr's template arguments be deduced?

丶灬走出姿态 提交于 2020-06-21 17:09:32
问题 When you have class template argument deduction available from C++17, why can't you deduce the template arguments of std::unique_ptr? For example, this gives me an error: std::unique_ptr smp(new D); That says "Argument list of class template is missing". Shouldn't the template arguments (at least the pointer type) be deducable? See this: any declaration that specifies initialization of a variable and variable template 回答1: I'm not going to repeat the rationale in @NathanOliver's great answer,

Can I add a deduction guide to `std` namespace?

十年热恋 提交于 2020-05-29 03:45:26
问题 Suppose I want to make a new deduction guide making the following possible ? std::string str; std::basic_string_view sv = str; Would that be an Ok customization ? 回答1: [namespace.std]/2.4: The behavior of a C++ program is undefined if it declares [...] a deduction guide for any standard library class template. 来源: https://stackoverflow.com/questions/58992794/can-i-add-a-deduction-guide-to-std-namespace