template-specialization

inheriting from class of specialized self?

女生的网名这么多〃 提交于 2019-12-01 19:48:10
Is this valid C++? template<class category> class any_iterator : public any_iterator<void> { public: typedef any_iterator<void> any_iter_void; any_iterator() : any_iter_void() {} }; template<> class any_iterator<void> { public: typedef any_iterator<void> any_iter_void; any_iterator() {} void foo() {}; }; int main() { any_iterator<int> a; a.foo(); } MSVC10 accepts it with no errors/warnings on \WALL , but gcc-4.5.1 complains: prog.cpp:3:5: error: invalid use of incomplete type 'class any_iterator' prog.cpp:2:11: error: declaration of 'class any_iterator' prog.cpp: In function 'int main()': prog

Creating a new primitive type

為{幸葍}努か 提交于 2019-12-01 18:15:55
问题 Is there a way to create a new type that is like one of the basic types (eg char), and can be implcitly converted between, but will resolve diffrently in templates, such that for example, the following code works? typedef char utf8; template<typename T>void f(T c); template<> void f<char>(char c) { std::cout << "ascii " << c << std::endl; } template<> void f<utf8>(utf8 c)//error C2766: explicit specialization; 'void f<char>(char)' has already been defined { std::cout << "utf8 " << c << std:

Creating a new primitive type

半腔热情 提交于 2019-12-01 18:08:00
Is there a way to create a new type that is like one of the basic types (eg char), and can be implcitly converted between, but will resolve diffrently in templates, such that for example, the following code works? typedef char utf8; template<typename T>void f(T c); template<> void f<char>(char c) { std::cout << "ascii " << c << std::endl; } template<> void f<utf8>(utf8 c)//error C2766: explicit specialization; 'void f<char>(char)' has already been defined { std::cout << "utf8 " << c << std::endl; } int main() { char c1 = 'x'; utf8 c2 = 'g'; f(c1); f(c2); } I'm thinking that it may be possible

Implementing multiparameter C++ template like behaviour on C# using Policy Pattern

无人久伴 提交于 2019-12-01 16:22:11
I'm trying to implement a c++ like template with C# generics and policy pattern based on this answer This is a sample of the pattern: interface ISomePolicy<T,U> { void _doSomething(U u); } class MyClass<T,U>: ISomePolicy<int, double>, ISomePolicy<int, int> { internal T myElement {get;set;} public MyClass(T Element) { myElement = Element; } void ISomePolicy<int, double>._doSomething(double u) { Console.WriteLine("this is int, double"); } void ISomePolicy<int, int>._doSomething(int u) { Console.WriteLine("this is int, int"); } } static class MyClassExtension { //What I want to do public static

Implementing multiparameter C++ template like behaviour on C# using Policy Pattern

陌路散爱 提交于 2019-12-01 15:18:07
问题 I'm trying to implement a c++ like template with C# generics and policy pattern based on this answer This is a sample of the pattern: interface ISomePolicy<T,U> { void _doSomething(U u); } class MyClass<T,U>: ISomePolicy<int, double>, ISomePolicy<int, int> { internal T myElement {get;set;} public MyClass(T Element) { myElement = Element; } void ISomePolicy<int, double>._doSomething(double u) { Console.WriteLine("this is int, double"); } void ISomePolicy<int, int>._doSomething(int u) { Console

Partial member function template specialisation and data member access

梦想的初衷 提交于 2019-12-01 06:21:30
I have a question regarding partial specialisation of templated member functions. Background: The goal is to compute descriptive statistics of large datasets which are too large to be hold in memory at once. Therefore I have accumulator classes for the variance and the covariance where I can push in the datasets piece by piece (either one value at a time or in larger chunks). A rather simplified version computing the arithmetic mean only is class Mean { private: std::size_t _size; double _mean; public: Mean() : _size(0), _mean(0) { } double mean() const { return _mean; } template <class T>

Partial member function template specialisation and data member access

邮差的信 提交于 2019-12-01 05:24:36
问题 I have a question regarding partial specialisation of templated member functions. Background: The goal is to compute descriptive statistics of large datasets which are too large to be hold in memory at once. Therefore I have accumulator classes for the variance and the covariance where I can push in the datasets piece by piece (either one value at a time or in larger chunks). A rather simplified version computing the arithmetic mean only is class Mean { private: std::size_t _size; double

How to simulate a partial specialization of selected member functions based on a template parameter that is an STL container?

瘦欲@ 提交于 2019-12-01 03:58:58
I am working with a class that uses STL containers as a template parameter. Not all containers provide the same methods though, so I am trying to figure out how I can specialise specific methods based on the container used. Example: template<typename container> class A { private: container m_container; public: void foo(); // does something container specific // more generic methods that work with any container }; The following code doexsn't compile saying "Can't match method specialisation", but this is approximately what I want to achieve: template<typename T> template<> void A<std::vector<T>

template metafunction for detecting template specialisations

人走茶凉 提交于 2019-12-01 03:46:56
Inspired by this question , i'm wondering if there is some compile-time check one can introduce to detect if two given template instantiations: template <typename T> class Templ... typedef Templ<std::string> stringInstance; typedef Templ<double> doubleInstance; are constructed from the same definition, or if they are built from different specializations of the Templ template so basically the hypothetical template function will behave like this: template <typename T> class Templ {} template <> class Templ<std::string> {} template <> class Templ<double> {} template <typename T1,typename T2>

template metafunction for detecting template specialisations

放肆的年华 提交于 2019-12-01 01:46:04
问题 Inspired by this question, i'm wondering if there is some compile-time check one can introduce to detect if two given template instantiations: template <typename T> class Templ... typedef Templ<std::string> stringInstance; typedef Templ<double> doubleInstance; are constructed from the same definition, or if they are built from different specializations of the Templ template so basically the hypothetical template function will behave like this: template <typename T> class Templ {} template <>