template-deduction

Why does template parameter deduction for T 'skips' the constness of array elements when function parameter is const reference to T?

孤者浪人 提交于 2019-12-07 08:23:49
问题 Let's consider those definitions: /*** full type information with typeid ***/ template <class> class Type{}; template <class T> std::string typeStr() { return typeid(Type<T>).name(); } /*** function template for parameter deduction ***/ template <class T> void func(const T &a) { std::cout << "Deduced type for T is: " << typeStr<T>() << std::endl; std::cout << "\targument type is: " << typeStr<decltype(a)>() << std::endl; } with pointers to const If the following statements are executed: const

Should deduction guide argument initialization considered by class template specialization deduction?

情到浓时终转凉″ 提交于 2019-12-06 19:50:34
问题 As a follow up of this question, I tested the behavior of both clang and gcc. It appears that the two compiler have a different interpretation of the c++ standard. In the example below, GCC refuses to compile, if a non copyable argument would need to be copied according to the deduction guide hypothetical constructor argument. Clang does not perform this check: #include <cstddef> struct not_copyable{ not_copyable()=default; not_copyable(const not_copyable&)=delete; }; struct movable{ movable(

Mixing aliases and template specializations

扶醉桌前 提交于 2019-12-06 11:48:15
问题 I'm trying to find the best method to have a kind of "object" that can be either specialized or "linked" to another type. For instance you cannot specialize a class to make it become a simple int, and you cannot use the keyword using to specialize classes. My solution is the following: 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 =

Why does template parameter deduction for T 'skips' the constness of array elements when function parameter is const reference to T?

二次信任 提交于 2019-12-05 16:59:33
Let's consider those definitions: /*** full type information with typeid ***/ template <class> class Type{}; template <class T> std::string typeStr() { return typeid(Type<T>).name(); } /*** function template for parameter deduction ***/ template <class T> void func(const T &a) { std::cout << "Deduced type for T is: " << typeStr<T>() << std::endl; std::cout << "\targument type is: " << typeStr<decltype(a)>() << std::endl; } with pointers to const If the following statements are executed: const int i=5, *ip=&i; func(ip); The output is: Deduced type for T is: 4TypeI**PKi**E So T is actually

Template non-type parameter deduction

怎甘沉沦 提交于 2019-12-05 11:42:32
Is it possible to deduce template value (not type) for a c++17 function? The function foo: template<int I> int foo() { return (I); } Can be called via: foo<5>(); And will return 5. Template types can be deduced through the type of a function argument. Is it possible to do the same in some way for the template value? For example: template<int I = x> int bar(const int x) { return (I); } This obviously wont work (because for one x is required before its declaration), but might there be some C++17 trick which would allow for this? I'd like to use this as a way of setting a constant expression

Why forwarding reference does not deduce to rvalue reference in case of rvalue?

半腔热情 提交于 2019-12-05 08:08:35
I understand that, given an expression initializing a forwarding/universal reference,lvalues are deduced to be of type T& and rvalues of type T (and not T&& ). Thus,to allow only rvalues, one need to write template<class T, enable_if<not_<is_lvalue_reference<T> >,OtherConds... > = yes> void foo(T&& x) {} and not, template<class T, enable_if<is_rvalue_reference<T>,OtherConds... > = yes> void foo(T&& x) {} My question is , why for forwarding references, rvalues are deduced to be of type T and not T&& ? I guess, if they are deduced as T&& then also same referencing collapsing rule works as T&& &&

The difference between int a[5] and int (&a)[5] in template parameter deduction

与世无争的帅哥 提交于 2019-12-05 02:03:09
This question is about functions that take arrays of statically known size. Take for example the following minimal program: #include <iostream> template<size_t N> void arrfun_a(int a[N]) { for(size_t i = 0; i < N; ++i) std::cout << a[i]++ << " "; } int main() { int a[] = { 1, 2, 3, 4, 5 }; arrfun_a<5>(a); std::cout << std::endl; arrfun_a<5>(a); return 0; } Which, when run, prints the expected result: 2 3 4 5 6 3 4 5 6 7 However, when I tried to have my compiler (VS 2010) deduce the 5 , it could not deduce template argument for 'int [n]' from 'int [5]' . A bit of research resulted in the

Class template argument deduction failed with derived class

旧街凉风 提交于 2019-12-05 01:03:59
#include <utility> template<class T1, class T2> struct mypair : std::pair<T1, T2> { using std::pair<T1, T2>::pair; }; int main() { (void)std::pair(2, 3); // It works (void)mypair(2, 3); // It doesn't work } Is the above well formed? Is it possible deduce the class template arguments in the second case if the constructors are being inherited? Are the constructors of std::pair participating in the creation of implicit deduction guides for mypair ? My compiler is g++ 7.2.0. I think this is a gcc bug (or at least a minor core language wording defect). Inherited constructors do count as

Class template argument deduction and default template parameters

馋奶兔 提交于 2019-12-04 16:47:33
问题 The following stripped down code doesn't work with the latest clang++5 but is accepted by g++7: template<typename Wrapped, typename U> struct wrapper; template<typename Wrapped, typename U=int> struct wrapper { wrapper() = default; // Automatic deduction guide constexpr explicit wrapper(Wrapped) noexcept {} }; int main() { struct {} dummy; constexpr auto wrapped = wrapper(dummy); } It fails with the following error messages: <source>:18:30: error: no viable constructor or deduction guide for

Deducing std::function with more than two args

橙三吉。 提交于 2019-12-04 14:28:01
I wonder why std::function is only aware of two-argument functions. I've written some code which is working well, but there are a number of limitations. Any feedback welcome. In particular, I suspect I'm reinventing the wheel. My code is on ideone and I will refer to it. For example, I can describe the type of main with: function_type_deducer(main).describe_me(); // Output: I return i and I take 2 arguments. They are of type: i PPc (where 'i' means 'int' and 'PPc' means pointer-to-pointer-to-char) Standard std::function doesn't work with functions with more than two args (see the last two