template-argument-deduction

Deduction failure of function call with explicit template argument list and [temp.arg.explicit]/3

送分小仙女□ 提交于 2020-02-20 07:50:59
问题 [temp.arg.explicit]/3 of the C++17 standard (final draft) says about deduction of function template arguments with explicitly specified template argument lists: In contexts where deduction is done and fails, or [...], if a template argument list is specified and it, along with any default template arguments, identifies a single function template specialization, then the template-id is an lvalue for the function template specialization. How does this apply to parameter packs? Consider template

Deduction failure of function call with explicit template argument list and [temp.arg.explicit]/3

馋奶兔 提交于 2020-02-20 07:50:27
问题 [temp.arg.explicit]/3 of the C++17 standard (final draft) says about deduction of function template arguments with explicitly specified template argument lists: In contexts where deduction is done and fails, or [...], if a template argument list is specified and it, along with any default template arguments, identifies a single function template specialization, then the template-id is an lvalue for the function template specialization. How does this apply to parameter packs? Consider template

Constructing std::function argument from lambda

一世执手 提交于 2020-02-10 15:15:18
问题 I have the following templated function (C++ latest standard is enabled in the compiler - but maybe 17 would be enough). #include <functional> template<typename TReturn, typename ...TArgs> void MyFunction(const std::function<TReturn(TArgs...)>& callback); int main() { MyFunction(std::function([](int){})); MyFunction([](int){}); } The first call compiles, when I explicitly convert it to std::function, but the second case does not. In the first case the template deduction is done automatically,

Why isn't argument deduction allowed in function return type?

青春壹個敷衍的年華 提交于 2020-02-01 04:12:05
问题 The most obvious answer could be - because the standard says so . That's fine, but I'm wrapping my head around it to understand the reasons behind this choice. Consider the following example: template<typename T> struct S { S(T) {} }; S f() { return 0; } int main() { auto s = f(); (void)s; } It fails to compile with errors like: error: use of class template 'S' requires template arguments; argument deduction not allowed in function return type Quite easy to fix, it isn't a problem, something

Template argument and deduction of std::function parameters

廉价感情. 提交于 2020-01-24 12:07:38
问题 Assume there is a template function foo() which accepts an arbitrary number of arguments. Given the last argument is always an std::function , how do I implement a foo() template shown below in a way that CbArgs would contain this std::function 's parameters? template<typename... InArgs, typename... CbArgs = ???> // ^^^^^^^^^^^^ void foo(InArgs... args) { ... } For example, CbArgs should be {int,int} if invoked like this: std::function<void(int,int)> cb; foo(5, "hello", cb); My first idea was

Abbreviated function template vs. function template with forwarding reference param

家住魔仙堡 提交于 2020-01-12 07:30:33
问题 What are the differences between function templates with forwarding reference parameters template<typename T> void Universal_func(T && a) { } and abbreviated function templates ? void auto_fun(auto && a) { } Can I replace Universal_func with auto_fun ? Is Universal_func a of auto_fun or are they equal? I have tested the below program. It seems that both are the same. template<typename T> void Universal_func(T && a) { } void auto_fun(auto && a) { } int main() { int i; const int const_i = 0;

Return double or complex<double> from template function

*爱你&永不变心* 提交于 2020-01-11 06:06:25
问题 I am writing some function templates to overload the * operator for a matrix class. I do a lot of work with matrices of type double and complex<double> . Is it possible to write a single template function that returns the correct type? For example: template<class T, class U, class V> matrix<V> operator*(const T a, const matrix<U> A) { matrix<V> B(A.size(1),A.size(2)); for(int ii = 0; ii < B.size(1); ii++) { for(int jj = 0; jj < B.size(2); jj++) { B(ii,jj) = a*A(ii,jj); } } return B; } I would

Function template cannot deduce type if types T and U are the same

北城以北 提交于 2020-01-07 05:50:24
问题 This question stems from an answer I received yesterday. For the following function, I am getting the error couldn't deduce template parameter ‘V’ if both T and U are std::complex<double> . If T and U are different, the function compiles and works as intended. template<class T, class U, class V> auto operator*(const T a, const matrix<U> A) -> decltype(std::declval<T>()*std::declval<U>()) { matrix<V> B(A.size(1),A.size(2)); for(int ii = 0; ii < B.size(1); ii++) { for(int jj = 0; jj < B.size(2)

How does the compiler deduce array size when defined as a template parameter?

随声附和 提交于 2020-01-06 15:19:47
问题 I am wondering how, in the following piece of code, the compiler deduces the arrsize template argument from the T (&arr)[arrsize] function argument. For example, when I pass a 4-element array to it, without mentioning the number 4 in my call to the function, it correctly determines the arrsize argument to be 4. However, if I pass the array normally (not as a reference to array), that is, if I change T (&arr)[arrsize] to T arr[arrsize] , it requires me to explicitly provide the arrsize

Template type deduction in function return type

孤街浪徒 提交于 2020-01-06 06:45:30
问题 This is in continuation with the question posted at Template type deduction for member variables and function arguments My .h file contains the following lines. #include <iostream> #include <complex> #include <typeinfo> template <typename T> class MyClass { template <typename T0> struct myTypeTraits { using type = T0; }; template <typename T0> struct myTypeTraits<std::complex<T0>> { using type = T0; }; public: using T0 = typename myTypeTraits<T>::type; void setVar1(const T0& v); void setVar2