template-specialization

Syntax for specialization of nested template class

让人想犯罪 __ 提交于 2019-11-28 10:01:07
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 eluding me. I have a suspicion that it is not possible to explicitly specialize the Iterator class

Explicit specialization of function templates causes linker error

随声附和 提交于 2019-11-28 09:20:50
Functions.h: #pragma once #include <iostream> template<class T> void TemplatedFunction(T* p) {} template<> void TemplatedFunction<float>(float* p) {} template<> void TemplatedFunction<char>(char* p) {} Functions.cpp: #include "Functions.h" void Test() { TemplatedFunction<float>(NULL); TemplatedFunction<char>(NULL); } main.cpp: #include "Functions.h" void Test(); int main() { Test(); return 0; } Build errors: main.obj : error LNK2005: "void __cdecl TemplatedFunction<float>(float *)" (??$TemplatedFunction@M@@YAXPAM@Z) already defined in Functions.obj main.obj : error LNK2005: "void __cdecl

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

三世轮回 提交于 2019-11-28 06:02:55
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 something fixed in C++0x? This would allow me for example, to specialize std::max , std::swap , std:

C++ Template Specialization with Constant Value

一笑奈何 提交于 2019-11-28 05:46:51
Is there a straightforward way for defining a partial specialization of a C++ template class given a numerical constant for one of the template parameters? I'm trying to create special constructors for only certain kinds of template combinations: template <typename A, size_t B> class Example { public: Example() { }; A value[B]; }; template <typename A, 2> class Example { public: Example(b1, b2) { value[0] = b1; value[1] = b2; }; }; This example won't compile, returning an error Expected identifier before numeric constant in the second definition. I've had a look through a number of examples

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

老子叫甜甜 提交于 2019-11-28 04:31:50
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 reference as they are all derived from a2de::Shape. So the specializations are declared as: template

Understanding (simple?) C++ Partial Template Specialization

回眸只為那壹抹淺笑 提交于 2019-11-28 03:51:43
Note: this seems to be a repost of a problem: C++ - Overload templated class method with a partial specilization of that method I have boiled down a problem I am having with C++ template specialization down to a simple case. It consists of a simple 2-parameter template class Thing , where I would like to specialize Thing<A,B>::doSomething() for B=int . #include <cstdio> // // A 3-parameter template class. // template <class A, class B> class Thing { public: Thing(A a, B b) : a_(a), b_(b) {} B doSomething(); private: A a_; B b_; }; // // The generic case works as expected. // template <class A,

Specializing function template for reference types

﹥>﹥吖頭↗ 提交于 2019-11-28 02:51:11
问题 Why is the output of this code : #include <iostream> template<typename T> void f(T param) { std::cout << "General" << std::endl ; } template<> void f(int& param) { std::cout << "int&" << std::endl ; } int main() { float x ; f (x) ; int y ; f (y) ; int& z = y ; f (z) ; } is General General General The third one is surprizing because the function was specialized exactly for int& Edit : I know that overloading might be a proper solution. I just want to learn the logic behind it. 回答1: The type of

Template class incomplete specialization

让人想犯罪 __ 提交于 2019-11-28 00:01:18
I came across an interesting point that I wasn't able to explain or find an explanation for. Consider the following template definition (compiled with mingw g++ 4.6.2): template <typename T, typename S> class Foo { public: void f(){} void g(){} }; Should we want to, we can fully specialize any single member function: template <> void Foo<char,int>::f() {} But partial specialization fails with an "invalid use of incomplete type 'class Foo<...>'" error: template <typename T, typename S> void Foo<T,S*>::f() { } template <typename T> void Foo<T,int>::f() { } And I can't figure out why. Is it a

How to decide if a template specialization exist

*爱你&永不变心* 提交于 2019-11-27 22:56:25
I would like to check if a certain template specialization exist or not, where the general case is not defined. Given: template <typename T> struct A; // general definition not defined template <> struct A<int> {}; // specialization defined for int I would like to define a struct like this: template <typename T> struct IsDefined { static const bool value = ???; // true if A<T> exist, false if it does not }; Is there a way to do that (ideally without C++11)? Thanks Using the fact that you can't apply sizeof to an incomplete type: template <class T, std::size_t = sizeof(T)> std::true_type is

Function template specialization importance and necessity

倾然丶 夕夏残阳落幕 提交于 2019-11-27 21:39:18
I read C++ Primer, and it says function template specialization is an advanced topic, but I am totally lost. Can anybody offer an example why function template specialization is important and necessary? Why don't function templates support partial specialization while class templates do? What's the underlying logic? Basically the idea is that you can write templates that behave in a generic way for the general case, but can still handle special cases. One example of where specialization is used is in std::vector . std::vector<bool> is a specialization that packs the bool elements such that