template-argument-deduction

Deduce first template parameter in multiple parameter template by parameter

感情迁移 提交于 2019-12-13 02:16:27
问题 First my question and then an explanation of what I'm trying to do as I might be approaching the problem wrong. Is it possible to deduce the first template parameter in a multi parameter template from parameters while specifying the other parameters. Example: template<class A, class B> B function(A object) { return B(A); } called like: function<BType>(AInstance); I could not find a way to make this work. EDIT: Another example might fit better to my problem below as indicated to me by the

Template type deduction for member variables and function arguments

僤鯓⒐⒋嵵緔 提交于 2019-12-12 18:16:43
问题 Consider the following implementation of a template class: template<class T> class MyClass { public: void setVar1(const T& v1) { var1 = v1; } void setVar2(const T1& v2) { var2 = v2; } T var1; T1 var2; }; If the template parameter T is a fundamental type (like float , double , or long double ) I would like to have T1 = T . If the template parameter T is std::complex<float> , I would like to have T=std::complex<float> and T1 = float . Similarly for std::complex<double> and std::complex<long

Template arguments deduction for function parameter pack followed by other parameters

眉间皱痕 提交于 2019-12-12 10:39:02
问题 Is the deduction for f1 and f2 ill-formed? template<class... T, class U> void f1(T..., U){} template<class... T> void f2(T..., int){} int main() { f1(1); f2(1); return 0; } g++ accepts both, clang only accepts f2 , and msvc rejects both. Related standard wording: [temp.deduct.call] When a function parameter pack appears in a non-deduced context ([temp.deduct.type]), the type of that parameter pack is never deduced. [temp.deduct.type]p5 The non-deduced contexts are: A function parameter pack

Template specialization and alias template deduction difference

南笙酒味 提交于 2019-12-12 07:51:24
问题 I'm struggling to understand how deduction works in the following case: template<class Category, Category code> struct AImpl { }; template<class Category, Category code> struct AHelper { using type = AImpl<Category, code>; }; template<class Category, Category code> using A = typename AHelper<Category, code>::type; template<int code> void doSomething(A<int, code> object) { } Following is the test code: A<int, 5> a1; doSomething(a1); // This does not compile doSomething<5>(a1); // This compiles

Take a reference if lvalue and make a copy if rvalue i.e. make rvalue persistent

落爺英雄遲暮 提交于 2019-12-11 07:59:31
问题 I'm pretty new in move and lvalue semantics. And I have the impression I'm doing it wrong. Here the code I want to be able to write once FunctContainer is implemented: std::function<double(double)> f = [](double x){return (x * x - 1); }; FunctContainer fc1 = FunctContainer(f); FunctContainer fc2 = FunctContainer([](double x){return (x * x - 1); }); I want to write FunctContainer 's ctors so that the lifetime of the function stored in fc1 is the one of f and the lifetime in fc2 of the

Why does “template argument deduction for class templates” not work on a plain struct?

依然范特西╮ 提交于 2019-12-11 06:02:02
问题 C++17 supports template argument deduction for class templates . Please see www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0091r3.html for detailed background information. However, the code below doesn't work as expected; #include <utility> template<typename T> struct A { T x; }; int main() { auto p = std::pair{ 1, 2 }; // ok, as expected. auto a = A{ 0 }; // // error : no viable constructor or deduction guide // for deduction of template arguments of 'A' // } My compiler is clang 5.0 with

Get input/output type of callable

廉价感情. 提交于 2019-12-10 23:08:31
问题 I have the following problem: template<typename Func, typename T_in = /*input type of Func */, typename T_out = /*output type of Func */> std::vector<T_out> foo( Func f, const std::vector<T_in>& input) { std::vector<T_out> res( input.size() ); for( size_t i = 0 ; i < input.size() ; ++i ) res[ i ] = f( input[ i ] ); return res; } int main() { // example for f(x) = x*x std::vector<float> input = { /* ... */ }; auto res = foo( [](float in){ return in*in; }, input ); return 0; } As you can see

Template argument deduction for references as arguments

旧街凉风 提交于 2019-12-10 19:59:56
问题 I am trying to profoundly understand Template Argument Deduction. One point I am not understanding is, how I should apply the rules in the standard here for the types A and P for the following case (there is sadly no example on cppreference.com, see below the relevant section) template<typename T> void foo(T t); void call_with_reference(int& r) { foo(r) } P is no reference typ: which gives P := T A := int& -> Match P and A which gives: T is deduced to int& which is cleary wrong. Where is the