template-argument-deduction

Mixing variadic template values and variadic deduced types

心不动则不痛 提交于 2019-12-23 07:37:12
问题 Is the following perfectly defined by the standard ? #include <iostream> template <unsigned int... Values, class... Types> void f(Types&&... values) { std::cout<<sizeof...(Values)<<" "<<sizeof...(Types)<<std::endl; } int main() { f<7, 5>(3); return 0; } It compiles well under g++ 4.8 but I wonder if it is normal. 回答1: From ISO C++ standard's current working draft 14.1 (11): A template parameter pack of a function template shall not be followed by another template >parameter unless that

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

孤街醉人 提交于 2019-12-22 04:26:08
问题 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

Class template argument deduction failed with derived class

浪子不回头ぞ 提交于 2019-12-22 01:53:47
问题 #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. 回答1: I think this is

Deducing std::function with more than two args

六眼飞鱼酱① 提交于 2019-12-21 20:25:46
问题 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

Is template-name<TT> a deduced context?

寵の児 提交于 2019-12-21 11:56:09
问题 [temp.deduct.type] paragraph 8 lists all deduced contexts, but it seems not to include template-name <TT> where template-name refers to a class template and TT refers to a template template argument. Is this a deduced context? If it is, why? If not, consider the following code: template<template<typename> class U, template<typename> class V> struct foo {}; template<template<typename> class U> struct foo<U, U> {}; int main() {} This code compiles under Clang 7.0.0 and GCC 8.0.1, which means

Is template-name<TT> a deduced context?

两盒软妹~` 提交于 2019-12-21 11:55:16
问题 [temp.deduct.type] paragraph 8 lists all deduced contexts, but it seems not to include template-name <TT> where template-name refers to a class template and TT refers to a template template argument. Is this a deduced context? If it is, why? If not, consider the following code: template<template<typename> class U, template<typename> class V> struct foo {}; template<template<typename> class U> struct foo<U, U> {}; int main() {} This code compiles under Clang 7.0.0 and GCC 8.0.1, which means

g++ and clang++ different behaviour deducing variadic template `auto` values

半城伤御伤魂 提交于 2019-12-21 07:22:36
问题 Another "who's right between g++ and clang++?" This time I'm convinced it's a g++ bug, but I ask for a confirm from standard gurus. Given the following code template <template <auto...> class Cnt, typename ... Types, Types ... Vals> void foo (Cnt<Vals...>) { } template <auto ...> struct bar { }; int main () { foo(bar<0, 1>{}); // compile both foo(bar<0, 1L>{}); // only clang++ compile; error from g++ } Live demo clang++ (8.0.0, by example) compile and link without problem where g++ (9.2.0, by

g++ and clang++ different behaviour deducing variadic template `auto` values

南楼画角 提交于 2019-12-21 07:21:47
问题 Another "who's right between g++ and clang++?" This time I'm convinced it's a g++ bug, but I ask for a confirm from standard gurus. Given the following code template <template <auto...> class Cnt, typename ... Types, Types ... Vals> void foo (Cnt<Vals...>) { } template <auto ...> struct bar { }; int main () { foo(bar<0, 1>{}); // compile both foo(bar<0, 1L>{}); // only clang++ compile; error from g++ } Live demo clang++ (8.0.0, by example) compile and link without problem where g++ (9.2.0, by

Do we still need to write the empty angle brackets when using transparent std function objects?

大憨熊 提交于 2019-12-21 06:46:20
问题 With class template argument deduction we can write: std::less Fn; However, G++ 8.2 rejects this code: #include <algorithm> #include <vector> #include <functional> int main() { std::vector v= { 1, 3, 2, 7, 5, 4 }; std::sort(v.begin(),v.end(),std::greater()); } emitting the following error: error: cannot deduce template arguments for 'greater' from () Clang++ 7.0 and MSVC 15.8.0 compile it without warnings. Which compiler is right? 回答1: GCC is wrong. There is already a bug report. [dcl.type

Partial template function specification in C++ works, but why?

旧巷老猫 提交于 2019-12-20 12:09:25
问题 I'm trying to find out whether partial specification of templated functions is part of the C++ standard, or whether this is something compiler specific. By partial specification, I mean specifying only the types the compiler can't deduce. So if I have a template function 'f' that takes 3 types, and one is used in a parameter and can be deduced, I might call 'f' with the form f<type, type>(parameter) Here's an example: #include <iostream> #include <tuple> #include <string> template<class A,