template-specialization

Partial specialization of a method in a templated class

百般思念 提交于 2019-11-27 03:53:31
问题 Given: struct A { virtual bool what() = 0; }; template<typename T, typename Q> struct B : public A { virtual bool what(); }; I want to partially specialize what like: template<typename T, typename Q> bool B<T, Q>::what() { return true; } template<typename Q> bool B<float, Q>::what() { return false; } But it appears that this isn't possible (is it in C++11?) so I tried SFINAE: template<typename T> typename std::enable_if<std::is_same<T, float>::value, bool>::type B<T>::what() { return true; }

Syntax for specialization of nested template class

非 Y 不嫁゛ 提交于 2019-11-27 03:20:43
问题 I'm trying to figure out the correct syntax for explicit specialization of a nested template class. The following code will better illustrate: struct Column_Major; struct Row_Major; template<size_t rows, size_t cols, typename T, typename Allocator> class Matrix { /* bunch of members */ template <typename storage = Column_Major> class Iterator { /* bunch of members */ }; }; I'd like to write an explicit specialization for template <> class Matrix<...>::Iterator<Row_Major , but the syntax is

Will specialization of function templates in std for program-defined types no longer be allowed in C++20?

安稳与你 提交于 2019-11-27 02:33:33
问题 Quote from cppreference.com: Adding template specializations It is allowed to add template specializations for any standard library |class (since C++20)| template to the namespace std only if the declaration depends on at least one program-defined type and the specialization satisfies all requirements for the original template, except where such specializations are prohibited. Does it mean, that starting from C++20, adding specializations of function templates to the std namespace for user

Class template specializations with shared functionality

为君一笑 提交于 2019-11-27 01:52:39
问题 I'm writing a simple maths library with a template vector type: template<typename T, size_t N> class Vector { public: Vector<T, N> &operator+=(Vector<T, N> const &other); // ... more operators, functions ... }; Now I want some additional functionality specifically for some of these. Let's say I want functions x() and y() on Vector<T, 2> to access particular coordinates. I could create a partial specialization for this: template<typename T> class Vector<T, 3> { public: Vector<T, 3> &operator+=

Why aren't template specializations allowed to be in different namespaces?

故事扮演 提交于 2019-11-27 01:10:48
问题 Please, see what I am trying to do: #include <iostream> namespace first { template <class T> class myclass { T t; public: void who_are_you() const { std::cout << "first::myclass"; } }; } namespace second { using first::myclass; template <> class myclass <int> { int i, j; public: void who_are_you() const { std::cout << "second::myclass"; } }; } This isn't allowed. Could you please, clarify why can't specializations be in different namespaces, and what are the available solutions? Also, is it

C++ template specialization, calling methods on types that could be pointers or references unambiguously

 ̄綄美尐妖づ 提交于 2019-11-27 00:22:27
问题 Summary Is there a way to call a class method on a templated type that could be a pointer or a reference without knowing which and not get compiler/linker errors? Details I have a templated QuadTree implementation that can take any of the following non-trivial user-defined types: //Abstract Base Class a2de::Shape //Derived Classes a2de::Point a2de::Line a2de::Rectangle a2de::Circle a2de::Ellipse a2de::Triangle a2de::Arc a2de::Spline a2de::Sector a2de::Polygon But they could be a pointer OR a

C++ templates specialization syntax

北战南征 提交于 2019-11-26 23:50:06
In C++ Primer Plus (2001, Czech Translation) I have found these different template specialization syntax: function template template <typename T> void foo(T); specialization syntax void foo(int param); // 1 void foo<int>(int param); // 2 template <> void foo<int>(int param); // 3 template <> void foo(int param); // 4 template void foo(int param); // 5 Googling a bit, I have found only No.3 examples. Is there any difference (in call, compiling, usage) among them? Are some of them obsolete/deprecated? Why not just use No.1? Nawaz Here are comments with each syntax: void foo(int param); //not a

Specialization of member function template after instantiation error, and order of member functions

[亡魂溺海] 提交于 2019-11-26 23:03:03
问题 The following bit of code fails to compile on gcc 4.5.3 struct Frobnigator { template<typename T> void foo(); template<typename T> void bar(); }; template<typename T> void Frobnigator::bar() { } template<typename T> void Frobnigator::foo() { bar<T>(); } template<> // error void Frobnigator::foo<bool>() { bar<bool>(); } template<> void Frobnigator::bar<bool>() { } int main() { } Error message: specialization of ‘void Frobnigator::bar() [with T = bool]’ after instantiation . I finally resolved

Specialization of templated member function in templated class

早过忘川 提交于 2019-11-26 22:58:32
I have a templated class with an templated member function template<class T> class A { public: template<class CT> CT function(); }; Now I want to specialize the templated member function in 2 ways. First for having the same type as the class: template<class T> template<> // Line gcc gives an error for, see below T A<T>::function<T>() { return (T)0.0; } Second for type bool: template<class T> template<> bool A<T>::function<bool>() { return false; } Here is how I am trying to test it: int main() { A<double> a; bool b = a.function<bool>(); double d = a.function<double>(); } Now gcc gives me for

“template<>” vs “template” without brackets - what's the difference?

人走茶凉 提交于 2019-11-26 22:42:48
问题 Suppose I've declared: template <typename T> void foo(T& t); Now, what is the difference between template <> void foo<int>(int& t); and template void foo<int>(int& t); semantically? And do template-with-no-brackets and template-with-empty-brackets have other semantics in other contexts? Related to: How do I force a particular instance of a C++ template to instantiate? 回答1: template <> void foo<int>(int& t); declares a specialization of the template, with potentially different body. template