function-templates

Function templates: Different specializations with type traits

☆樱花仙子☆ 提交于 2019-12-05 07:17:45
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 about doing it this way: template <typename T, typename ENABLE = void> void foo(){ //Do something }

C++ template specialization on functions

北城以北 提交于 2019-12-05 00:56:04
I'm playing around with template specialization, and I've found an issue I can't seem to solve; this is my code: template<int length, typename T> void test(T* array) { ... test<length-1>(array); } template<typename T> void test<0>(T* array) { return; } So what I'm trying to do, is to pass the length, of what's to be processed in the template. The problem is, that the compilation of this, well outputs forever: a.cpp:83:43: error: template-id 'test<0>' in declaration of primary template a.cpp: In function 'void test(T*) [with int length= -0x000000081, T = int]': a.cpp:77:9: instantiated from

Templated Functions.. ERROR: template-id does not match any template declaration

我与影子孤独终老i 提交于 2019-12-04 03:38:28
问题 I have written a function template and an explicitly specialized templated function which simply takes in 3 arguments and calculates the biggest among them and prints it. The specialized function is causing an error,whereas the template works fine. But I want to work with char* type . This is the error I get=> error: template-id ‘Max<>’ for ‘void Max(char, char, char)’ does not match any template declaration Following is my code: template <typename T> void Max(T& a,T& b,T& c) { if(a > b && a

Template default argument loses its reference type

本小妞迷上赌 提交于 2019-12-03 14:22:44
问题 Consider #include <iostream> #include <type_traits> template <class T, class ARG_T = T&> T foo(ARG_T v){ return std::is_reference<decltype(v)>::value; } int main() { int a = 1; std::cout << foo<int>(a) << '\n'; std::cout << foo<int, int&>(a) << '\n'; } I'd expect the output to be 1 in both cases. But in the first case it's 0: consistent with the default being class ARG_T = T rather than class ARG_T = T& . What am I missing? 回答1: For foo<int>(a) , ARG_T is being deduced from a , and is not

Why is template constructor preferred to copy constructor?

余生长醉 提交于 2019-12-03 10:34:18
问题 #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

Template default argument loses its reference type

依然范特西╮ 提交于 2019-12-03 04:12:06
Consider #include <iostream> #include <type_traits> template <class T, class ARG_T = T&> T foo(ARG_T v){ return std::is_reference<decltype(v)>::value; } int main() { int a = 1; std::cout << foo<int>(a) << '\n'; std::cout << foo<int, int&>(a) << '\n'; } I'd expect the output to be 1 in both cases. But in the first case it's 0: consistent with the default being class ARG_T = T rather than class ARG_T = T& . What am I missing? For foo<int>(a) , ARG_T is being deduced from a , and is not taken from the default template argument. Since it's a by value function parameter, and a is an expression of

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

非 Y 不嫁゛ 提交于 2019-12-03 02:08:13
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, class B, class C> std::tuple<A, B> test(C c) { // do something based on c, return tuple with types A and

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

我只是一个虾纸丫 提交于 2019-12-02 04:33:20
问题 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

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

倖福魔咒の 提交于 2019-12-02 04:17:39
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 because the template parameter is not matching the definition. Line 5 has error because the template

Parameterization and “function template partial specialization is not allowed”

喜你入骨 提交于 2019-12-02 00:42:25
问题 This is a continuation of What is the function parameter equivalent of constexpr? In the original question, we are trying to speed-up some code that performs shifts and rotates under Clang and VC++. Clang and VC++ does not optimize the code well because it treats the shift/rotate amount as variable (i.e., not constexpr ). When I attempt to parameterize the shift amount and the word size, it results in: $ g++ -std=c++11 -march=native test.cxx -o test.exe test.cxx:13:10: error: function