specialization

explicit specialization: syntax error?

你离开我真会死。 提交于 2019-12-12 05:26:28
问题 I am writing a program that requires a template function having an array of items, and number of items in that array as arguments. The function needs to return the largest item in said array. The program should also have a specialization (what I'm having issues with) which checks for the longest string in the array, if an array of strings is entered. Here are my prototypes: template <typename T> T compare(T const arr1[], int n); template <> const char *compare<const char *>(const char *const

Can i merge these two classes via specialization?

孤者浪人 提交于 2019-12-12 01:44:52
问题 Here is a class i wrote to mock .NET properties. It appears to do what i want. However instead of using Property1 and Property2 can i write Property and have it figure out which of the two classes i want? #include <cstdio> template <class T> class Property{ protected: Property(const Property&p) {} Property() {} public: virtual Property& operator=(const Property& src)=0; virtual Property& operator=(const T& src)=0; virtual operator T() const=0; }; template <class T> class Property1 : public

error C2761 member function redeclaration not allowed

寵の児 提交于 2019-12-11 18:03:43
问题 I've encountered a problem(error C2761) while writing specializations for a class. My classes are as follows: class Print{ public: typedef class fontA; typedef class fontB; typedef class fontC; typedef class fontD; template<class T> void startPrint(void) { return; }; virtual bool isValidDoc(void) = 0; }; I have a class QuickPrint which inherits the Print class: class QuickPrint : public Print { ... }; The error occurs when I try to write specializations for the startPrint method: template<> /

How to overload/specialize template class function to handle arithmetic types and a container-class

一笑奈何 提交于 2019-12-11 17:42:27
问题 I am trying to create a template class with a memberfunction which can handle arithmetic datatypes (int, char, float ...) and a container-class like Eigen::DenseBase<> or std::vector<> Code to demonstrate my idea: template <typename T>class myClass{ ... void foo(T); ... }; template <typename T> void myClass<T>::foo(T){ //Function for arithmetic Datatypes } //Specialization does not work - What is the correct (best?) approach? template <> void myClass<T>::foo(<Eigen::DenseBase<T>){ //Function

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

Specializing a class template constructor

半世苍凉 提交于 2019-12-10 23:27:23
问题 I'm messing around with template specialization and I ran into a problem with trying to specialize the constructor based on what policy is used. Here is the code I am trying to get to work. #include <cstdlib> #include <ctime> class DiePolicies { public: class RollOnConstruction { }; class CallMethod { }; }; #include <boost/static_assert.hpp> #include <boost/type_traits/is_same.hpp> template<unsigned sides = 6, typename RollPolicy = DiePolicies::RollOnConstruction> class Die { // policy type

Template specialization or conditional expressions?

北城以北 提交于 2019-12-10 18:06:46
问题 I am deep into a new project which I address with a bunch of templates and specializations of them. Now, after a day without programming, I find myself asking whether it is really worth the extra lines of code. The question is: What are the advantages of specialization? Is this: template <int i> class A {}; template <> class A <1> { void foo() {/* something */} }; template <> class A <2> { void foo() {/* something else*/} }; template <> class A <3> { void foo() {/* even different*/} }; In any

Cannot overload function

空扰寡人 提交于 2019-12-10 17:53:12
问题 So I've got a templatized class and I want to overload the behavior of a function when I have specific type, say char. For all other types, let them do their own thing. However, c++ won't let me overload the function. Why can't I overload this function? I really really do not want to do template specialization, because then I've got duplicate the entire class. Here is a toy example demonstrating the problem: http://codepad.org/eTgLG932 The same code posted here for your reading pleasure:

Inlining causes specialized member function of template class overriding virtual functions to get overlooked

微笑、不失礼 提交于 2019-12-10 17:13:36
问题 I wanted to share a strange example with you guys that I stumbled upon and that kept me thinking for two days. For this example to work you need: triangle-shaped virtual inheritance (on member function getAsString() ) member function specialization of a template class (here, Value<bool>::getAsString() ) overriding the virtual function (automatic) inlining by the compiler You start with a template class that virtually inherits a common interface - i.e. a set of virtual functions. Later, we

Specializing inner template with default parameters

五迷三道 提交于 2019-12-10 14:55:00
问题 I'm having trouble specializing an inner template when it's parameters are all known. Here's an example: template < typename T0 > struct outer { template < typename T1 = void, typename T2 = void > struct inner { typedef T1 type; }; }; template < typename T0 > template < typename T1 > struct outer<T0>::inner<double,T1> { typedef int type; }; This works just fine. If I instead specify the inner template like so, it does not: template < typename T0 > template < > struct outer<T0>::inner<double