function-templates

Deduction failure of function call with explicit template argument list and [temp.arg.explicit]/3

馋奶兔 提交于 2020-02-20 07:50:27
问题 [temp.arg.explicit]/3 of the C++17 standard (final draft) says about deduction of function template arguments with explicitly specified template argument lists: In contexts where deduction is done and fails, or [...], if a template argument list is specified and it, along with any default template arguments, identifies a single function template specialization, then the template-id is an lvalue for the function template specialization. How does this apply to parameter packs? Consider template

Function template parameters failing to convert type during compilation

瘦欲@ 提交于 2020-01-06 11:20:14
问题 While trying to use a function template that calls a class's specialized static function template it is failing to convert its parameter from the template parameter list. Here is the function that I'm calling in main: template<class Engine, typename Type, template<typename = Type> class Distribution, class... DistParams> Type randomGenerator( RE::SeedType seedType, std::size_t seedValue, std::initializer_list<std::size_t> list, DistParams... params ) { static Type retVal = 0; static Engine

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

Function Template Specialization Error

こ雲淡風輕ζ 提交于 2019-12-25 04:19:12
问题 I am trying to specialize my function template for list of int pointers. template <typename typ> void sortowanie(typ *tablica, int rozmiar, Komparator<typ> *komparator) { int p; for(int j = rozmiar - 1; j > 0; j--) { p = 1; for(int i = 0; i < j; i++) if(komparator->porownaj(tablica[i], tablica[i + 1])) { typ pom = tablica[i]; tablica[i] = tablica[i + 1]; tablica[i + 1] = pom; p = 0; } if(p) break; } }; template<> void sortowanie<int *>(int **tablica, int rozmiar, Komparator<int> *komparator)

Template parameter default to a later one

99封情书 提交于 2019-12-22 06:30:19
问题 This link doesn't answer my question so I'll ask it here: Basically I want to write a template function template <typename Out, typename In> Out f(In x); Here I always need to specify Out when calling f . I don't want to do it every time, so I basically want template <typename Out = In, typename In> Out f(In x); Which means if I don't specify Out , it will default to In . However, this is not possible in C++11. So my question is, is there any way to achieve the effect: calling f(t) will

Function templates: Different specializations with type traits

穿精又带淫゛_ 提交于 2019-12-22 05:38:20
问题 Considering class templates, it is possible to provide template specializations for certain types of groups using type traits and dummy enabler template parameters. I've already asked that earlier. Now, I need the same thing for function templates: I.e., I have a template function and want a specialization for a group of types, for example, all types that are a subtype of a class X . I can express this with type traits like this: std::enable_if<std::is_base_of<X, T>::value>::type I thought

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,

Can a template function be called with missing template parameters in C++ ?

匆匆过客 提交于 2019-12-20 03:52:50
问题 This is an interview question, which has been done. Which line has error ? #include<iostream> template<class T> void foo(T op1, T op2) { std::cout << "op1=" << op1 << std::endl; std::cout << "op2=" << op2 << std::endl; } template<class T> struct sum { static void foo(T op1, T op2) { std::cout << "sum=" << op2 << std::endl ; } }; int main() { foo(1,3); // line1 foo(1,3.2); // line2 foo<int>(1,3); // line3 foo<int>(1, '3') ; // line 4 sum::foo(1,2) ; // line 5 , return 0; } Line 2 has error

Variadic function template with pack expansion not in last parameter

假如想象 提交于 2019-12-17 04:08:16
问题 I am wondering why the following code doesn't compile: struct S { template <typename... T> S(T..., int); }; S c{0, 0}; This code fails to compile with both clang and GCC 4.8. Here is the error with clang: test.cpp:7:3: error: no matching constructor for initialization of 'S' S c{0, 0}; ^~~~~~~ test.cpp:4:5: note: candidate constructor not viable: requires 1 argument, but 2 were provided S(T..., int); ^ It seems to me that this should work, and T should be deduced to be a pack of length 1. If

Variadic function template with pack expansion not in last parameter

做~自己de王妃 提交于 2019-12-17 04:08:11
问题 I am wondering why the following code doesn't compile: struct S { template <typename... T> S(T..., int); }; S c{0, 0}; This code fails to compile with both clang and GCC 4.8. Here is the error with clang: test.cpp:7:3: error: no matching constructor for initialization of 'S' S c{0, 0}; ^~~~~~~ test.cpp:4:5: note: candidate constructor not viable: requires 1 argument, but 2 were provided S(T..., int); ^ It seems to me that this should work, and T should be deduced to be a pack of length 1. If