specialization

C++ template specialization via a base class

旧巷老猫 提交于 2019-12-23 03:02:02
问题 I want to be able to make the compiler shout when i call a constructor of foo with a class that is NOT derived from _base*. The current code allows only for foo<_base*> itself. Any easy solution ? class _base { public: // ... }; class _derived: public _base { public: // ... }; template <typename T> class foo { public: foo () { void TEMPLATE_ERROR; } }; template <> foo<_base*>::foo () { // this is the only constructor } main-code: foo<_base*> a; // should work foo<_derived*> b; // should work

C++ template specialization via a base class

我只是一个虾纸丫 提交于 2019-12-23 03:01:07
问题 I want to be able to make the compiler shout when i call a constructor of foo with a class that is NOT derived from _base*. The current code allows only for foo<_base*> itself. Any easy solution ? class _base { public: // ... }; class _derived: public _base { public: // ... }; template <typename T> class foo { public: foo () { void TEMPLATE_ERROR; } }; template <> foo<_base*>::foo () { // this is the only constructor } main-code: foo<_base*> a; // should work foo<_derived*> b; // should work

No generated code for explicitly specialized template even with explicit instantiation

℡╲_俬逩灬. 提交于 2019-12-22 05:52:33
问题 I'm getting consistent behavior from both gcc 4.8.3 and clang 3.2, but do not understand why it is happening. Despite the fact that I have an explicit instantiation for a class template, the code is not being generated and I get an undefined symbol when I am using a fully specialized instance of the template. I have a simple class template definition in a file 'temp.hpp' #pragma once template <typename T1> class C { public: C (T1 c) : d_c(c) {}; ~C () = default; void print (); private: T1 d_c

Partial template specialization ambiguity

喜欢而已 提交于 2019-12-21 07:15:14
问题 I cant see why the statement in main is ambiguous. template<class T, class U, int I> struct X { void f() { cout << "Primary template" << endl; } }; template<class T, int I> struct X<T, T*, I> {void f() { cout << "Partial specialization 1" << endl;}}; template<class T, class U, int I> struct X<T*, U, I> {void f() { cout << "Partial specialization 2" << endl;}}; template<class T> struct X<int, T*, 10> {void f() { cout << "Partial specialization 3" << endl;}}; template<class T, class U, int I>

C++ template class specialization: why do common methods need to be re-implemented

半城伤御伤魂 提交于 2019-12-21 07:13:11
问题 In the sample: #include <iostream> using namespace std; class B { public: virtual void pvf() = 0; }; template <class T> class D : public B { public: D(){} virtual void pvf() {} private: string data; }; template <> class D<bool> : public B { public: D(); virtual void pvf(){ cout << "bool type" << endl; } }; int main() { D<int> d1; D<bool> d2; } I get the following error: test.cpp:(.text+0x1c): undefined reference to `D<bool>::D()' Note that the reason I don't just specialize the D() by itself

C++ template specialization of constructor

别来无恙 提交于 2019-12-20 21:52:27
问题 I have a templated class A<T, int> and two typedefs A<string, 20> and A<string, 30>. How do I override the constructor for A<string, 20> ? The following does not work: template <typename T, int M> class A; typedef A<std::string, 20> one_type; typedef A<std::string, 30> second_type; template <typename T, int M> class A { public: A(int m) {test= (m>M);} bool test; }; template<> one_type::one_type() { cerr << "One type" << endl;} I would like the class A<std::string,20> to do something that the

C++ partial template specialization syntax

岁酱吖の 提交于 2019-12-20 01:47:02
问题 for primary template: template<typename A, typename B> class MyClass {... with template specialization, what is the difference between template<typename A, typename B> class MyClass<int, float> {... and template<> class MyClass<int, float> {... 回答1: template<typename A, typename B> class MyClass<int, float> {... should be not allowed. Indeed, if you specify the formal parameters A and B , your template should be using them. The second case is just normal: you say that you are making

C++ partial template specialization syntax

让人想犯罪 __ 提交于 2019-12-20 01:46:11
问题 for primary template: template<typename A, typename B> class MyClass {... with template specialization, what is the difference between template<typename A, typename B> class MyClass<int, float> {... and template<> class MyClass<int, float> {... 回答1: template<typename A, typename B> class MyClass<int, float> {... should be not allowed. Indeed, if you specify the formal parameters A and B , your template should be using them. The second case is just normal: you say that you are making

Is making a function template specialization virtual legal?

房东的猫 提交于 2019-12-18 19:43:32
问题 In C++, a function template specialization is supposed to act exactly like a normal function. Does that mean that I can make one virtual? For example: struct A { template <class T> void f(); template <> virtual void f<int>() {} }; struct B : A { template <class T> void f(); template <> virtual void f<int>() {} }; int main(int argc, char* argv[]) { B b; A& a = b; a.f<int>(); } Visual Studio 2005 gives me the following error: fatal error C1001: An internal error has occurred in the compiler.

Function template specialization with a template class [duplicate]

回眸只為那壹抹淺笑 提交于 2019-12-18 17:25:42
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: partial specialization of function template I can't find anywhere a solution for my problem, because if I search with the keywords I come up with would give me solutions suited for different problems. I understand that this must been asked before, just can't find a solution. Suppose I have a function template: template<class any> print(any value); I can specialize it like this for let's say a int : template<>