template-argument-deduction

Template type deduction in function return type

戏子无情 提交于 2020-01-06 06:44:25
问题 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

Function template deduction l-value reference and universal reference

独自空忆成欢 提交于 2020-01-05 04:15:06
问题 Let's say I have the function copy : template <typename Buf> void copy( Buf&& input_buffer, Buf& output_buffer) {} In which input_buffer is a universal reference and output_buffer is an l-value reference. Reference collapsing rules make sure input_buffer is indeed, regardless of the deduced type of Buf , an universal reference and output_buffer is indeed an l-value reference. However, I wonder how type Buf is deduced here. I found out that copy is passed an r-value as input_buffer , (and an l

Template Argument Deduction Failure and Function Parameters/Arguments Mismatch

左心房为你撑大大i 提交于 2020-01-01 04:45:12
问题 Consider the following program: template <class T> struct A { using X = typename T::X; }; template <class T, typename A<T>::X* = nullptr> void f(T, int); void f(...); template <class T> void g(T, int, typename A<T>::X* = nullptr); // # void g(...); int main() { // f(0, nullptr); // error g(0, nullptr); // ok } g(0, nullptr) compiles while f(0, nullptr) does not (tested under GCC trunk and Clang trunk on Godbolt). It seems that during the template argument deduction process of # , the compiler

Deducing a user-defined-value template argument (C++2a, P0732R2)

百般思念 提交于 2019-12-30 05:08:28
问题 I am trying to get the value of a template parameter of a user-defined class deduced (http://wg21.link/p0732r2), using GCC 9.1 with -std=c++2a. struct user_type { int a; constexpr user_type( int a ): a( a ){} }; template< user_type u > struct value {}; template< user_type u > void f( value< u > arg ){} void g(){ f( value< user_type( 0 ) >() ); // error here } compiler explorer: https://godbolt.org/z/6v_p_R I get the error: source>:8:30: note: template argument deduction/substitution failed:

What's the point of “boost::mpl::identity<T>::type” here?

旧街凉风 提交于 2019-12-28 20:42:52
问题 I was checking the implementation of clamp in boost: template<typename T, typename Pred> T const & clamp ( T const& val, typename boost::mpl::identity<T>::type const & lo, typename boost::mpl::identity<T>::type const & hi, Pred p ) { // assert ( !p ( hi, lo )); // Can't assert p ( lo, hi ) b/c they might be equal return p ( val, lo ) ? lo : p ( hi, val ) ? hi : val; } If I look up the documentation, identity returns the template argument unchanged. The identity metafunction. Returns X

Template deduction: why it works with inheritance (when it fails with conversion)?

*爱你&永不变心* 提交于 2019-12-24 05:57:23
问题 I am trying to understand the C++ language behaviour that I have observed (I understood the first half of it). My setup: 2 templated classes: A and C . A can be converted to C but not the other way around. They have some common behaviour, so I was searching to implement some logic only with C and rely on the conversion from A to C to make A behave the same way as C . The examples uses an operator overloading, but the discussion I think is the same with a function or a method. What I tried

Template deduction: why it works with inheritance (when it fails with conversion)?

邮差的信 提交于 2019-12-24 05:57:01
问题 I am trying to understand the C++ language behaviour that I have observed (I understood the first half of it). My setup: 2 templated classes: A and C . A can be converted to C but not the other way around. They have some common behaviour, so I was searching to implement some logic only with C and rely on the conversion from A to C to make A behave the same way as C . The examples uses an operator overloading, but the discussion I think is the same with a function or a method. What I tried

g++ and clang++ are both bugged with template function parameter pack expansion?

只谈情不闲聊 提交于 2019-12-24 05:32:35
问题 This is a follows up of another question where an answer point my attention to Templates [temp.param] 17.1.17 (last C++17 draft, but I suppose also preceding standardizations) where is stated that A template parameter pack that is a pack expansion shall not expand a parameter pack declared in the same template-parameter-list. with an example of this limitation template <class... T, T... Values> // error: Values expands template type parameter struct static_array; // pack T within the same

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

倖福魔咒の 提交于 2019-12-24 03:49:08
问题 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 ..

Template deduction fails with argument after parameter pack

淺唱寂寞╮ 提交于 2019-12-23 17:53:26
问题 I have this function: template <typename... Args> void f(Args... args, int last) { } Template deduction fails if I call it without explicit template parameters: f(2, 2); // candidate expects 1 argument, 2 provided But giving the explicit template parameters for the parameter pack works: f<int>(2, 2); // compiles fine Even though logically speaking, the compiler should be able to deduce that the parameter pack consists of all but the last argument types. How would I fix this? 回答1: [temp.deduct