function-templates

Why function template cannot be partially specialized?

最后都变了- 提交于 2019-12-17 03:03:01
问题 I know the language specification forbids partial specialization of function template. I would like to know the rationale why it forbids it? Are they not useful? template<typename T, typename U> void f() {} //allowed! template<> void f<int, char>() {} //allowed! template<typename T> void f<char, T>() {} //not allowed! template<typename T> void f<T, int>() {} //not allowed! 回答1: AFAIK that's changed in C++0x. I guess it was just an oversight (considering that you can always get the partial

Why function template cannot be partially specialized?

混江龙づ霸主 提交于 2019-12-17 03:02:47
问题 I know the language specification forbids partial specialization of function template. I would like to know the rationale why it forbids it? Are they not useful? template<typename T, typename U> void f() {} //allowed! template<> void f<int, char>() {} //allowed! template<typename T> void f<char, T>() {} //not allowed! template<typename T> void f<T, int>() {} //not allowed! 回答1: AFAIK that's changed in C++0x. I guess it was just an oversight (considering that you can always get the partial

Partial template specialization by overloading

青春壹個敷衍的年華 提交于 2019-12-13 06:26:20
问题 I created a simple round template function with an extra template argument that defines the type the rounded value needs to be casted to before returning. template <typename T, typename U> T round(U val) { T result; if (val >= 0) result = (T)(floor(val + (U)(.5))); else result = (T)(ceil( val - (U)(.5))); return result; } int a = round<int>(5.5); // = 6 // no compiler warnings But I also want the possibility to leave the extra template argument so that you don't have to insert the type you

Where to put a member function template

杀马特。学长 韩版系。学妹 提交于 2019-12-12 11:35:20
问题 An aspect of C++ that periodically frustrates me is deciding where templates fit between header files (traditionally describing the interface) and implemention (.cpp) files. Templates often need to go in the header, exposing the implementation and sometimes pulling in extra headers which previously only needed to be included in the .cpp file. I encountered this problem yet again recently, and a simplified example of it is shown below. #include <iostream> // for ~Counter() and countAndPrint()

Default template parameter in Visual Studios 2012

ε祈祈猫儿з 提交于 2019-12-12 05:09:32
问题 This question is a followup after this one. The actual problem is that default template parameters for function templates are not supported by Visual Studios 2012 as indicated by this list. Since default template parameters are not supported by Visual Studios 2012, is there any workaround to have the same result without it? So is it possible to define a template function such as template <typename T, typename Ret = T> Ret round(T val, Ret ret = Ret()) { return static_cast<Ret>( (val >= 0) ?

Passing template typedef as argument to a function template

为君一笑 提交于 2019-12-11 09:52:56
问题 I am trying to pass a template typedef as argument to a function template. However I get following errors: TestTemplates.cpp:11: error: expected unqualified-id before ‘&’ token TestTemplates.cpp:11: error: expected unqualified-id before ‘&’ token TestTemplates.cpp:11: error: expected initializer before ‘&’ token TestTemplates.cpp:25: error: ‘func’ was not declared in this scope #include <iostream> #include <vector> template<class T> struct MyVector { typedef std::vector<T> Type; }; template

Is There a Way to Declare a typename for a Templatized Function?

我们两清 提交于 2019-12-11 09:30:31
问题 So, I have this templatized function (which I know is ugly to look at.) My intention was not to default the template parameter though, my intention was to create a typename derived from T that could be used in caster that the user could not assign to. My question is how do I create a typename for a templatized function which the user cannot pass as an argument? As an example: template <typename T> typename R = std::conditional<sizeof(T) == 4, char, short>; R foo(T bar){return R(bar);} Clearly

64-bit G++ 4.6.3 doesn't treat longs as long longs in specialised function templates, even though they're the same size. Is this a bug?

不羁的心 提交于 2019-12-11 04:54:37
问题 Consider the following code: #include <iostream> #include <cinttypes> template<class T> void f(); template<> inline void f<long long>() { std::cout<<"f<long long>()"<<std::endl; } int main(int , char** ) { std::cout<<"sizeof(long)="<<sizeof(long)<<std::endl; std::cout<<"sizeof(long long)="<<sizeof(long long)<<std::endl; f<int64_t>(); return 0; } 32-bit G++ 4.6.3 compiles this successfully and produces the output: sizeof(long)=4 sizeof(long long)=8 f<long long>() Compiling under 64-bit G++ 4.6

How Can I Avoid Explicitly Specializing Templatized Functions With Argument Dependent Lookup

試著忘記壹切 提交于 2019-12-11 01:47:23
问题 So I've written an answer which uses a templatized function to select object type. I've defined the types: struct pt { double t; double e; double c_vis; double c_invis; }; struct pt_weighted : pt { double sigma; }; And my templatized function looks like: template <typename T> void foo() { for(T point; dataFile >> point;) { set.curve.push_back(point); // store point data_numPoints++; // collect some stats set.curveAvg += point.e; } } Given that minimizator_weighted decides which type to use at

C++: template function with explicitly specified reference type as type parameter

别来无恙 提交于 2019-12-10 21:08:25
问题 I was playing with C++ template type deduction and managed to compile this little program. template<typename T> void f(const T& val) { val = 1; } int main() { int i = 0; f<int&>(i); } It compiles on all major compilers but I do not understand why. Why can f assign to val , when val is explicitly marked const ? Is it bug in these compilers or is it valid behavior according to the C++ standard? 回答1: §8.3.2 [dcl.ref]/p6: If a typedef-name (7.1.3, 14.1) * or a decltype-specifier (7.1.6.2) denotes