why Specialized template class need forward declaration? - c++

such is the code:
template<typename,int> class Uoo; //without this will result in complie error,why?
template<typename T>
class Uoo<T,1>
{
};
int main(){
return 0;
}
why Specialized template class need forward declaration?

The following code is a specialisation of a template.
template<typename T>
class Uoo<T,1>
{
};
But you haven't said what the unspecialised form is, and the language requires you to do that. So you need to add the prototype:
template<typename,int> class Uoo;
You don't actually need to declare the unspecialised form since an instance of it is never required. So a prototype is sufficient.

It's not actually a forward declaration that you're making. What you are doing is first defining the "pattern" of the templated class and then you're defining a specialized context or version of it. The better question is, if you didn't have a non-specialized case, then what would be the point of the 2nd template parameter?

As declared, template<typename T> class Uoo<T,1> is a partial specialization of template<typename,int> class Uoo; it fixes the int parameter to 1. It cannot be a partial specialization of a template that doesn't exist.
You could make your "real" class template self-sufficient by writing
template<typename T>
class Uoo
{
...
};

Related

How do I define methods outside of the declaration for my specialization of a non-type template class?

The title is a mouthful, but basically I wrote something like this:
enum EnumType{ValA, ValB};
template<EnumType> class A {};
template<>
class A<ValA>
{
private:
double param;
public:
A(double param);
};
template<>
A<ValA>::A(double param)
{
// Do Stuff
}
and when I try to compile it I get:
error: template-id 'A<>' for 'A<(EnumType)0u>::A(double)' does not
match any template declaration
Am I doing this wrong?
After searching online for similar cases, I tried to remove template<> (even though I don't understand why this would work), but then I get
multiple definition of 'A<(EnumType)0u>::A(double)'
I guess that I can replace template<> by inline (I tried and it compiles), but that doesn't feel like the proper way to do it (or if it is, I don't understand why).
Can someone explain to me what is wrong with what I wrote, why changing this seems to work, and what's the proper way to do it ?
Can someone explain to me what is wrong with what I wrote, why changing this seems to work, and what's the proper way to do it ?
The standard says:
Members of an explicitly specialized class template are defined in the same manner as members of normal classes, and not using the template<> syntax.
Therefore, in your case you must use:
A<EnumType::ValA>::A(double param)
{
// Do Stuff
}
No template<> at all is just fine. That's because you are actually specializing a (special) member function (the constructor) of an explicitly specialized class template.
See it on coliru.
It would have been different if no explicit specialization was given.
As a minimal working example:
enum EnumType{ValA, ValB};
template<EnumType> class A
{
private:
double param;
public:
A(double param);
};
template<>
A<EnumType::ValA>::A(double)
{
// Do Stuff
}
int main() {
A<EnumType::ValA> a{0.};
}
In this case, template<> is required before the definition of the constructor because you are not defining a specialization of a member function of an already specialized class template.
You missed a semicolon (;) at the the end of class definition.
And the non template member function can be defined this way:
A<ValA>::A(double param) {
// Do Stuff
}
Informally, the template parameter list is only written when necessary, for example, for defining a member function template of a class template, the two template parameter list should all be written
template<class U, class V>
class A{
template <class T>
A();
};
template<class U, class V>
template <class T>
A<U, V>::A() {}
and a empty template parameter list is needed for a explicit specialisation of function template (which, i guess, is the reason why you use so here), informally because it tells the compiler that this is not a function overloading.

C++ class templates: redundancy in non-inline member functions?

I just started learning C++ and am looking at the tutorials online on templates.
The example I'm looking at:
// class templates
#include <iostream>
using namespace std;
template <class T>
class mypair {
T a, b;
public:
mypair (T first, T second)
{a=first; b=second;}
T getmax ();
};
template <class T>
T mypair<T>::getmax ()
{
T retval;
retval = a>b? a : b;
return retval;
}
int main () {
mypair <int> myobject (100, 75);
cout << myobject.getmax();
return 0;
}
What I don't complete understand is the heading for getmax():
template <class T>
T mypair<T>::getmax ()
A few questions:
Is it possible to include fewer or more template parameters in the member function definition than in the template class? So, if mypair actually took in class T and class U, then when you define getmax, is it possible to only pass class T as a template parameter?
If that's not the case, then is it redundant to have class T and mypair<T>? Since you cannot have different template parameters anyway?
Sorry if this isn't completely clear.
Thanks!
When you declare your template class mypair, you are actually declaring three fairly independent templates: a class template for mypair, a function template for mypair::mypar constructor and a function template for mypair::getmax member function. Thise templates are, of course, not completely unrelated, since getmax is a member of mypair, but they still have a significant degree of independence. They are instantiated independently, they can be specialized independently.
For example, you can explicitly specialize mypair::getmax without specializing mypair. This means that there's really no redundancy in what you are talking about. The definition of mypair::getmax is a definition of a [fairly] independent function template. The "basic" definition of that function template has to share the number and the types of its template parameters with class template (so the answer to your "less or more" question is "no"), but nevertheless they are template parameters that belong to function template.
However, you are not required to provide the "basic" definition for mypair::getmax at all. You already declared it in the class, so if you wish, you can omit the "basic" definition completely and proceed to defining the specializations right away
template<> int mypair<int>::getmax() {
// whatever
}
template<> double mypair<double>::getmax() {
// whatever
}
Yes, it's possible to have a different numbers of template parameters in more advanced situations. For now, though, just write it like that.

How do I fully specialize a function template with a class template?

template <typename T>
void foo(T t)
{
... // do stuff with type T
}
template <typename T>
class class_template
{
// class body
};
template<> // failed attempt at full specialization
void foo(class_template<T> t) // which doesn't work of course
{
//full specialization for all classes of class_template
}
In the above code how do I explicitly specialize function foo with a class template?
In the above code how do I explicitly specialize function foo with a class template?
You cannot. This is the whole point of partial specialisations. But they don’t work for functions.
You have two solutions:
Overload the function. This usually works.
Refer the work to a class template, which can be partially specialised. That is, inside your function, call a (static) function in a class template, and specialise that.

Can one template class can be friend of another template class in c++?

I have a two template class, templateClass1 and templateClass2. I want to use private variables and methods of templateClass1 in templateClass2. Is it possible to do so by using friend keyword in c++ ?
Sumit
I know this post is probably dead, but for other people who stumble upon this...
templateClass1.h
template <class T> class templateClass2; // forward declare
template <typename T>
class templateClass1 {
friend templateClass2<T>;
};
templateClass2.h
template <class T> class templateClass1; // forward declare
template <typename T>
class templateClass2 {
friend templateClass1;
}
It is possible to have a friends of any type, but a template is not a type until its template arguments have been supplied. So in general you would have to have a specialization for each full type you wish to be friends with. This will push you toward attempting to pass the type to be friend as a template parameter, but you can not supply a template type that will be friended.
for ex. this is illegal
template <class T>
class A
{
friend class T;
};
with those stipulations it makes it very difficult to do anything meaning full with templates and friendedness.

Template specialization with struct and bool

I have a template class in which I am specializing a couple of methods. For some reason, when I added a specialization for a struct, it seems to be conflicting with the specialization for bool. I am getting a type conversion error because it is trying to set the struct = bool (resolving to the wrong specialization). Here is some code
.h:
typedef struct foo {
...
}
template <class T> class bar {
template <class T> void method1() {...}
template <> void method1<bool>() {...}
template <> void method1<foo>() {...}
}
.cpp
template class bar<bool>;
template class bar<foo>;
I am getting the error inside method1<bool> because it is setting T=foo instead of resolving it to method1<foo>.
Any ideas?
The first part of your code is already incorrect. C++ does not support explicit specialization of "nested" (member) templates without explicit specialization of the enclosing template.
In the context of your code, it is illegal to explicitly specialize template method method1 without explicitly specializing the entire class template bar.
If your member template function member1 depended on some parameters, you could use overloading instead of template specialization as a workaround. But since it doesn't, you have to redesign you templates somehow. What you do above is, once again, illegal in C++.
The errors you get further down can easily be (and most probably are) induced by that original problem.
P.S. The description of the problem you posted implies that your code compiles. What you posted should not compile for the reasons described above. This suggests that you are posting fake code. Post real code.
(EDITED)
You may try the following, which delegates the method implementation to a templated helper class.
.h:
typedef struct Foo {
...
}
template<class T_Bar, class T2> struct BarMethod1;
template <class T> class Bar
{
template<class T2> void method1(...)
{
BarMethod1<Bar, T2>(...)(...);
}
}
template <class T_Bar, class T2> class BarMethod1
{void operator()(...){...}};
template <class T_Bar> class BarMethod1<T_Bar, bool>
{void operator()(...){...}};
template <class T_Bar> BarMethod1<T_Bar, Foo>
{void operator()(...){...}};
.cpp
template class Bar<bool>;
template class BarMethod1<Bar<bool>, bool>;
template class BarMethod1<Bar<bool>, Foo>;
template class Bar<Foo>;
template class BarMethod1<Bar<Foo>, bool>;
template class BarMethod1<Bar<Foo>, Foo>;