template-argument-deduction

How does template argument deduction work when an overloaded function is involved as an argument?

孤人 提交于 2019-12-19 05:12:57
问题 This is the more sophisticated question mentioned in How does overload resolution work when an argument is an overloaded function? Below code compiles without any problem: void foo() {} void foo(int) {} void foo(double) {} void foo(int, double) {} // Uncommenting below line break compilation //template<class T> void foo(T) {} template<class X, class Y> void bar(void (*f)(X, Y)) { f(X(), Y()); } int main() { bar(foo); } It doesn't appear a challenging task for template argument deduction -

Can I generate a function without providing arguments?

ぐ巨炮叔叔 提交于 2019-12-18 09:36:15
问题 So c++17 has std::function Deduction Guides so given: int foo(); I can do: std::function bar(foo); But I'm stuck on a c++14 compiler. There I have to do something more like: function<int()> bar(foo) . I was wondering if there was a way to create a std::function without passing the function pointer and explicitly providing the function signature? So for example make_pair will deduce the type of it's return from it's arguments. I was wondering if I could write something similar for function s

Deduction guide and variadic templates

主宰稳场 提交于 2019-12-17 20:45:55
问题 Consider the following code: #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[]

Deduction guides and variadic class templates with variadic template constructors - mismatched argument pack lengths

家住魔仙堡 提交于 2019-12-17 14:46:34
问题 Consider the following class definition and deduction guide: template <typename... Ts> struct foo : Ts... { template <typename... Us> foo(Us&&... us) : Ts{us}... { } }; template <typename... Us> foo(Us&&... us) -> foo<Us...>; If I try to instantiate foo with explicit template arguments , the code compiles correctly: foo<bar> a{bar{}}; // ok If I try to instantiate foo through the deduction guide ... foo b{bar{}}; g++7 produces a compiler error: prog.cc: In instantiation of 'foo<Ts>::foo(Us ..

Deduce template argument from std::function call signature

北慕城南 提交于 2019-12-17 05:11:21
问题 Consider this template function: template<typename ReturnT> ReturnT foo(const std::function<ReturnT ()>& fun) { return fun(); } Why isn't it possible for the compiler to deduce ReturnT from the passed call signature? bool bar() { /* ... */ } foo<bool>(bar); // works foo(bar); // error: no matching function call 回答1: std::function<bool()> bar; foo(bar); // works just fine C++ can't deduce the return type from your function bar because it would have to know the type before it could find all the

Template deduction for function based on its return type?

夙愿已清 提交于 2019-12-17 03:40:25
问题 I'd like to be able to use template deduction to achieve the following: GCPtr<A> ptr1 = GC::Allocate(); GCPtr<B> ptr2 = GC::Allocate(); instead of (what I currently have): GCPtr<A> ptr1 = GC::Allocate<A>(); GCPtr<B> ptr2 = GC::Allocate<B>(); My current Allocate function looks like this: class GC { public: template <typename T> static GCPtr<T> Allocate(); }; Would this be possible to knock off the extra <A> and <B> ? 回答1: That cannot be done. The return type does not take part in type

What are template deduction guides and when should we use them?

不打扰是莪最后的温柔 提交于 2019-12-17 03:30:54
问题 The C++17 standard introduces "template deduction guides". I gather they're something to do with the new template argument deduction for constructors introduced in this version of the standard, but I haven't yet seen a simple, FAQ-style explanation of what they are and what they're for. What are template deduction guides in C++17? Why (and when) do we need them? How do I declare them? 回答1: Template deduction guides are patterns associated with a template class that tell the compiler how to

Template type derivation

雨燕双飞 提交于 2019-12-14 03:08:01
问题 I need to implement a class, say 'MyClass' using templates. template<class T> class MyClass { public: T var1; T1 var2; }; There are two member variables var1 and var2. If the class template argument, 'T', is fundamental type (eg: float, double or long double), the types of both the variables var1 and var2 should be the same as the template argument. That is T1 = T in the above example. But if the template argument is std::complex<T> , I would like to have T var1; std::complex<T> var2; How to

Difference between references and values in deduction guides

会有一股神秘感。 提交于 2019-12-13 14:19:32
问题 Considering type A : template <typename T, size_t N> struct A { T arr[N]; }; Is there any difference between C++17 user-defined deduction guides template <typename T, typename ... Ts> A(const T&, const Ts& ...) -> A<T, 1 + sizeof...(Ts)>; and template <typename T, typename ... Ts> A(T, Ts ...) -> A<T, 1 + sizeof...(Ts)>; ? Or, in other words is there any difference between const references and values in deduction guides? Please note that the question is not about template function type

Partial template specialization by overloading

青春壹個敷衍的年華 提交于 2019-12-13 06:26:20
问题 I created a simple round template function with an extra template argument that defines the type the rounded value needs to be casted to before returning. template <typename T, typename U> T round(U val) { T result; if (val >= 0) result = (T)(floor(val + (U)(.5))); else result = (T)(ceil( val - (U)(.5))); return result; } int a = round<int>(5.5); // = 6 // no compiler warnings But I also want the possibility to leave the extra template argument so that you don't have to insert the type you