天道酬勤,学无止境

function-templates

Overload of template function with one that accepts lambdas

Apologies for jumping here, but my google-foo is not up to snuff for template functions (or function templates?) and their overloads with function templates (or template functions?). I'm trying to overload a function template with another function template that can take a lambda and I can't figure out why it's not selected Here's my exact scenario template< typename buffer_t, typename value_t > void fill(buffer_t buffer, const value_t value) { std::fill(buffer.begin(), buffer.end(), value); } //partial specialization for buffers template< typename sample_t, stride_t stride=1u > void fill

2022-04-30 11:43:34    分类:问答    c++11   overloading   function-templates

Overloading the End of Recursion for a Variable Length Template Function

François Andrieux gave me a good workaround for this Visual Studio 2017 problem. I was trying to build on his answer like so: template<class T, size_t N> ostream& vector_insert_impl(ostream& lhs, const char*, const T& rhs) { return lhs << at(rhs, N); } template<class T, size_t N, size_t... I> ostream& vector_insert_impl(ostream& lhs, const char* delim, const T& rhs) { return vector_insert_impl<T, I...>(lhs << at(rhs, N) << delim, delim, rhs); } template <typename T, size_t... I> ostream& vector_insert(ostream& lhs, const char* delim, const T& rhs, index_sequence<I...>) { return vector_insert

2022-04-29 07:45:24    分类:问答    c++   recursion   variadic-templates   variadic-functions   function-templates

partial specialization of function templates

In the below code snippet, template<typename T1> void func(T1& t) { cout << "all" << endl; } template<typename T2> void func(T2 &t) { cout << "float" << endl; } // I do not want this // template<> void func(float &t) int main() { int i; float f; func(i); // should print "all" func(f); // should print "float" return 0; } I would like to have the templates modified which by passing any type other than float will print "all" and passing float will print "float". I do not want template specialization, instead have partial specialization which will act accordingly based on input type. How should i

2022-04-26 14:38:20    分类:问答    c++   templates   template-specialization   function-templates

How do I create a template function for controls of a form?

问题 此语句将更改表单对象的位置。 lblMessage.Location = new Point(0,0); 我想编写一个可以定位任何表单对象的通用模板函数。 我想出了这个,但它是无效的: public void ChangePosition<T>(T form_object) { form_object.Location = new Point(0,0); } 我这样称呼它: ChangePosition(lblMessage); 错误:“T”不包含“Location”的定义,并且找不到接受“T”类型的第一个参数的扩展方法“Location”(您是否缺少 using 指令或程序集引用?) 我需要在模板功能上提及某种接口吗? 如何调用泛型类型的扩展方法? 回答1 您不需要通用方法,您可以这样做: public void ChangePosition(Control form_object) { form_object.Location = new Point(0,0); } 表单的所有控件的基类是具有Location属性的Control 。 回答2 public void ChangePosition<T>(T form_object) where T : Control { form_object.Location = new Point(0,0); }

2022-04-24 17:16:05    分类:技术分享    c#   winforms   generics   interface   function-templates

Function Template Specialization Error

问题 我正在尝试将我的函数模板专门用于 int 指针列表。 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) { 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])) { int

2022-04-20 19:27:59    分类:技术分享    c++   function   templates   function-templates

Why is template constructor preferred to copy constructor?

#include <iostream> struct uct { uct() { std::cerr << "default" << std::endl; } uct(const uct &) { std::cerr << "copy" << std::endl; } uct( uct&&) { std::cerr << "move" << std::endl; } uct(const int &) { std::cerr << "int" << std::endl; } uct( int &&) { std::cerr << "int" << std::endl; } template <typename T> uct(T &&) { std::cerr << "template" << std::endl; } }; int main() { uct u1 ; // default uct u2( 5); // int uct u3(u1); // template, why? } coliru Template overload of the constructor fits to both declarations (u2 and u3). But when int is passed to the constructor, a non-template overload

2022-04-04 08:38:07    分类:问答    c++   copy-constructor   overload-resolution   function-templates   constructor-overloading

How do I create a template function for controls of a form?

This statement will change the position of a form object. lblMessage.Location = new Point(0,0); I would like to write a generic template function that can position any form object. I came up with this, but it is invalid: public void ChangePosition<T>(T form_object) { form_object.Location = new Point(0,0); } and I call it like this: ChangePosition(lblMessage); Error: 'T' does not contain a definition for 'Location' and no extension method 'Location' accepting a first argument of type 'T' could be found (are you missing a using directive or an assembly reference?) Do I need to mention some kind

2022-04-04 04:00:16    分类:问答    c#   winforms   generics   interface   function-templates

Function Template Specialization Error

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) { int p; for(int j = rozmiar - 1; j > 0; j--) { p = 1; for(int i = 0; i < j; i++) if(komparator-

2022-03-24 15:30:35    分类:问答    c++   function   templates   function-templates