function-templates

Interface and template functions

流过昼夜 提交于 2019-12-10 20:40:33
问题 I'm trying to have interface over two different classes where implementation of a function is in the subclass. It works for regular functions, but unfortunately not for template functions. See example: import std.conv; import std.stdio; interface Num { T num(T)(); } class A : Num { T num(T)() { return 5.to!T; } } class B : Num { T num(T)() { return 2.to!T; } } void main() { auto a = new A(); auto b = new B(); Num somea = a; Num someb = b; writeln(a.num!int()); writeln(somea.num!int());

Define friend function template of class template

好久不见. 提交于 2019-12-10 16:32:00
问题 I want to define a function template of a class template. The code looks like this. template<int M> struct test{ private: int value; template<int N = 2 * M> friend auto foo(test const t){ test<N> r; r.value = t.value; return r; } }; int main(){ test<1> t; foo(t);// expected to return test<2> foo<1>(t);// expected to return test<1> foo<3>(t);// expected to return test<3> } But it won't compile. Compared with previous problems, the following lists the differences. The result of the function

Overloading the End of Recursion for a Variable Length Template Function

时光怂恿深爱的人放手 提交于 2019-12-10 14:58:13
问题 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&

Is it possible to invoke an injected friend template function?

℡╲_俬逩灬. 提交于 2019-12-08 09:33:38
问题 To be consistent with other (non-template) functions in a class I wanted to define and invoke a friend template function. I can define it with no problem (see function t below). namespace ns{ struct S{ void m() const{} friend void f(S const&){} template<class T> friend void t(S const&){} }; template<class T> void t2(S const& s){} } However later I am not able to invoke this t function in any way? int main(){ ns::S s; s.m(); f(s); // t<int>(s); // error: ‘t’ was not declared in this scope (I

C++ Template type deduction for temporary value

情到浓时终转凉″ 提交于 2019-12-07 11:50:47
问题 #include <iostream> #include <vector> using namespace std; template <typename T> void wrapper(T& u) { g(u); } class A {}; void g(const A& a) {} int main() { const A ca; wrapper(ca); wrapper(A()); // Error } Hi I have a question on why the last statement gives a compiler error. :18:10: error: cannot bind non-const lvalue reference of type 'A&' to an rvalue of type 'A' wrapper(A()); I thought that the template type T would be deduced as const A& as the ca is also deduced as const A& . Why the

Function template specialization and the Abrahams/Dimov example

不想你离开。 提交于 2019-12-07 04:42:44
问题 (I'm assuming knowledge of the Abrahams/Dimov example in this question.) Assume there is some 3rd-party code in a header that like this, which you cannot modify: template<class T> void f(T); // (1) base template 1 template<class T> void f(T *); // (2) base template 2 template<> void f<>(int *); // (3) specialization of (2) The question is: If I have been given the declarations above as-is, is it possible for me to now specialize the base template 1 for the case where T = int * (for example)?

Is a friend function template defined in the class available for lookup? clang++ and g++ disagree

99封情书 提交于 2019-12-06 21:24:08
问题 Here is the code: struct foo { template<typename T = void> friend foo f() { return {}; } }; int main() { auto x = f(); // clang++ can't find it, g++ can. } clang++ 3.4 gives: fni2.cpp:8:12: error: use of undeclared identifier 'f' auto x = f(); // clang++ can't find it, g++ can. ^ 1 error generated. g++ 4.9.0 compiles it, but I don't think it should have. This is a related question, but there was no definitive answer. Section 15.4.2/2,4 discuss this, but neither of them say anything to suggest

C++ template specialization on functions

纵然是瞬间 提交于 2019-12-06 17:49:58
问题 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:

C++ Template type deduction for temporary value

人盡茶涼 提交于 2019-12-06 02:08:43
#include <iostream> #include <vector> using namespace std; template <typename T> void wrapper(T& u) { g(u); } class A {}; void g(const A& a) {} int main() { const A ca; wrapper(ca); wrapper(A()); // Error } Hi I have a question on why the last statement gives a compiler error. :18:10: error: cannot bind non-const lvalue reference of type 'A&' to an rvalue of type 'A' wrapper(A()); I thought that the template type T would be deduced as const A& as the ca is also deduced as const A& . Why the type deduction fails in this case? I thought that the template type T would be deduced as const A& as

Template parameter default to a later one

落花浮王杯 提交于 2019-12-05 08:41:53
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 instantiate f<T,T>(t) or more generally f<typename SomeThing<T>::type, T> calling f<U>(t) will instantiate f