template-argument-deduction

function template parameter loses const when template argument is explicity passed?

♀尐吖头ヾ 提交于 2021-02-08 15:38:09
问题 template <typename T> void test(const T& x) {} int a {}; int& ref = a; const int& c_ref = a; test(c_ref) // T = int, x = const int& test<int&>(ref); // T = int& , x = int& Why does the function template parameter x loses the const qualifier? 回答1: In the explicit (non-deduced) instantiation test<int&>(ref); this is the (theoretical) signature you get void test<int&>(const (int&)& x) which shows that the const -qualification applies to the whole (int&) , and not only the int . const applies to

Compiler discrepencies in deduction of template parameter of nested alias template

寵の児 提交于 2021-02-08 14:47:29
问题 Consider the following code snippet where we we're trying to deduce the template parameter to the function foobar() : struct Bar { static constexpr auto size = 5; }; template <class T, class=std::make_index_sequence<T::size>> struct FooList; template <class T, std::size_t... Idxs> struct FooList<T, std::integer_sequence<std::size_t, Idxs...>> { }; struct Wrapper { template <class T> using list_type = FooList<T>; }; template <class T> void foobar(typename Wrapper::template list_type<T>) { }

return type deduction of recursive function

微笑、不失礼 提交于 2021-02-07 06:52:17
问题 Recently, I read Barry's answer to this question Recursive lambda functions in C++11: template <class F> struct y_combinator { F f; // the lambda will be stored here // a forwarding operator(): template <class... Args> decltype(auto) operator()(Args&&... args) const { // we pass ourselves to f, then the arguments. // [edit: Barry] pass in std::ref(*this) instead of *this return f(std::ref(*this), std::forward<Args>(args)...); } }; // deduction guide template <class F> y_combinator(F) -> y

deduction guides for std::array

ぃ、小莉子 提交于 2021-02-05 06:16:45
问题 I go through the book C++ template unique quide and I try to understand how the deduction guides for std::array works. Regarding the definition of the standard the following is the declaration template <class T, class... U> array(T, U...) -> array<T, 1 + sizeof...(U)>; For example if in main a array created as std::array a{42,45,77} How the deduction takes place? Thank you 回答1: How the deduction takes place? It's simple. Calling std::array a{42,45,77} match array(T, U...) with T = decltype(42

How to emulate deduction guides for template aliases?

旧时模样 提交于 2021-02-04 17:53:06
问题 Consider the following: template <typename T, std::size_t N> struct my_array { T values[N]; }; We can provide deduction guides for my_array , something like template <typename ... Ts> my_array (Ts ...) -> my_array<std::common_type_t<Ts...>, sizeof...(Ts)>; Now, suppose that my_array<T, 2> has some very special meaning (but only meaning, the interface & implementation stay the same), so that we'd like to give it a more suitable name: template <typename T> using special = my_array<T, 2>; It

Auto return type of template and ambiguity

▼魔方 西西 提交于 2021-02-04 12:56:05
问题 I have an overloaded template function: template<typename T1, typename T2> auto overMax(T1 a, T2 b) { std::cout << __FUNCSIG__ << std::endl; return b < a ? a : b; } template<typename RT, typename T1, typename T2> RT overMax(T1 a, T2 b) { std::cout << __FUNCSIG__ << std::endl; return b < a ? a : b; } If I call it like this: auto a = overMax(4, 7.2); // uses first template auto b = overMax<double>(4, 7.2); // uses second template everything works perfect, but auto c = overMax<int>(4, 7.2); //