template<typename T>
class C
{
void f() { }
};
/*template<typename T>
void C<T*>::f() { }*/
template<>
void C<int*>::f() { }
If we remove comment, code will not compile. I know this (and i also know, that we should have partial specialization for C<T*>), but I cannot find words in standard, which explains such behaviour. I reread 14 par of standard few times. Can you give me a quote or par of standard, that explains this?
EDIT.
template<typename T>
class C
{
template<typename U>
struct S { };
};
// #1
/*template<typename T>
class C<T*>
{
template<typename U>
struct S { };
};*/
// #2
/*template<typename T>
template<typename U>
struct C<T*>::S<U*> { };*/
template<>
template<typename U>
struct C<int*>::S<U*> { };
If we remove only comment next then #2 - code will not compile.
Here is the standard quote about what may be specialized expliclitly, from 14.7.3/1:
An explicit specialization of any of the following:
— function template
— class template
— member function of a class template
— static data member of a class template
— member class of a class template
— member enumeration of a class template
— member class template of a class or class template
— member function template of a class or class template
can be declared by a declaration introduced by template<>;
Unless explicitly allowed, you cannot partially specialize anything, and member functions of class templates are not explicitly allowed. Only class templates may be specialized partially (as described in 14.5.5).
(Note that a member class template of an explicitly specialized class template is itself a class template.)
Related
Given the following definitions:
template <typename T>
class A {
public:
void f();
};
template <typename T>
void
A<T>::f()
{}
template <typename T>
class B {};
How would I partially specialize A<B<T>>::f, i.e. f for some B<T>? I'm basically looking for the right magic to substitute the ??? below
template <???>
void
A<B<T>>::f()
{}
You can have an explicit specialization, from [temp.expl.spec]:
An explicit specialization of any of the following:
— ...
— member function of a class template
— ...
can be declared by a declaration introduced by template<>
That is:
template <>
void A<B<int>>::f() {
std::cout << "B\n";
}
But you cannot have a partial specialization of a member function of a class template. You would have to partially specialize the entire class:
template <typename T>
class A<B<T>> {
public:
void f() {
std::cout << "B\n";
}
// ... all other members you want in A<B<T>> ...
};
You cannot partially specialize a member function (nor in fact any function). You need to partially specialize the whole class:
template<typename T>
class A<B<T>>
{
// implement member functions for this specialization here
};
If you must have:
template <typename T>
void A<B<typename T>>::f() {}
then your only choice is to partially specialize A.
template <typename T> class A<B<T>>
{
public:
void f();
};
C++11 has Alias Templates, allowing you do do something like:
template<T>
using AB = A<B<T>>;
Then you can refer to AB<T> instead of A<B<T>>.
Unfortunately, you can't use that for specialization..
So seems to me the answer to your question is: You can't do that, but it's a shame.
I read in many articles, that for class template when specializing
member template, the class that containing specialized member template also shall be explicitly specialized. Is there a point about it in standard and is there any reason to have such restriction?
I mean under the hood.
Why this is not allowed.
template <typename T>
class A
{
template <typename U>
void foo()
{}
};
template <typename T>
template <>
void A<T>::foo<int>()
{}
[temp.expl.spec]/16:
In an explicit specialization declaration for a member of a class
template or a member template that appears in namespace scope, the
member template and some of its enclosing class templates may remain
unspecialized, except that the declaration shall not explicitly
specialize a class member template if its enclosing class templates
are not explicitly specialized as well. [ Example:
template <class T1> class A {
template<class T2> class B {
template<class T3> void mf1(T3);
void mf2();
};
};
template <> template <class X>
class A<int>::B {
template <class T> void mf1(T);
};
template <> template <> template<class T>
void A<int>::B<double>::mf1(T t) { }
template <class Y> template <>
void A<Y>::B<double>::mf2() { } // ill-formed; B<double> is specialized
// but its enclosing class template A is not
— end example ]
I have code such as
template <class T> class Widget
{
void fun() {}
}
//Okay: specialization of a member function of widget
template <> void Widget<char>:: fun()
{
void fun() {}
}
But, below is error as I am been told. But not understand why.
template<class T, class U> class Gadget
{
void fun() {}
}
//Error! cannot partially specialize a member function of Gadget
template<class U> void Gadget<char,U>::fun()
{
..specialized implementation
}
Why is the second wrong? how to change it to make it right?
thanks!!!!
It is impossible to partially specialize just one single member function, you have to partially specialize the whole class. That's how things work in C++.
The reason is that you cannot have partially specialized functions, and member functions are themselves functions. By partially specializing the whole class, the member functions will "look" like templates with fewer types (in that partial specialized class).
Why you cannot have partially specialized functions is another story, and I don't have a good answer/understanding why is this enforced.
About making it work, why don't you partially specialize the class, then re-define only the function that you need.
One approach is to move that single function to a helper class template, which can be partially specialized:
template<class T, class U> class Gadget;
template<class T, class U>
struct Gadget_fun
{
static void do_it(Gadget<T,U>* This) {}
};
template<class T, class U> class Gadget
{
friend class Gadget_fun<T,U>;
void fun() { Gadget_fun<T,U>::do_it(this); }
};
template<class U>
struct Gadget_fun<char, U>
{
static void do_it(Gadget<char,U>* This)
{
//..specialized implementation
}
};
This way you don't have to duplicate all the other members as you would to specialize Gadget itself.
is it possible to somehow make a partial template specification a friend class? I.e. consider you have the following template class
template <class T> class X{
T t;
};
Now you have partial specializations, for example, for pointers
template <class T> class X<T*>{
T* t;
};
What I want to accomplish is that every possible X<T*> is a friend class of X<S> for ANY S. I.e. X<A*> should be a friend of X<B>.
Of course, I thought about a usual template friend declaration in X:
template <class T> class X{
template <class S> friend class X<S*>;
}
However, this does not compile, g++ tells me this:
test4.cpp:34:15: error: specialization of 'template<class T> class X' must appear at namespace scope
test4.cpp:34:21: error: partial specialization 'X<S*>' declared 'friend'
Is this not possible at all or is there some workaround?
The reason why I am asking is that I need a constructor in X<T*> that creates this class from an arbitrary X<S> (S must be a subtype of T).
The code looks like this:
template <class T> class X<T*>{
T* t;
template<class S>
X(X<S> x) : t(&(x.t)) {} //Error, x.t is private
}
Now, the compiler complains, of course, that x.t is not visibile in the constructor since it is private. This is why I need a partial specialization friend class.
In C++, you can grant access beyond private on four levels.
completely public access (see pmr's answer)
access within inheritance hierarchy (protected, irrelevant here)
to a base template friend (see this answer)
to a non-template or fully specialized friend (too weak to solve your use case)
There is no middle way between the two latter kinds of friendship.
From §14.5.4 of the C++ standard:.
Friend declarations shall not declare partial specializations.
The following declaration will allow you to implement what you need. It gives you a free hand to access any specialization of your template from any other specialization, but still only within X. It is slightly more permissive than what you asked for.
template<class T> class X
{
template<class Any> friend class X;
public:
...
};
We can define a getter protected by a key defined in X.
#include <type_traits>
template <class T> class X{
T t;
public:
struct Key {
template<typename S>
Key(const X<S>&) {
static_assert(std::is_pointer<S>::value, "Not a pointer");
}
};
const T& get(Key) const { return t; }
T& get(Key) { return t; }
};
template <class T> class X<T*> {
T* t;
public:
template<class S>
X(X<S>& x) : t(&(x.get(typename X<S>::Key(*this)))) {}
};
int main()
{
X<int> x1;
X<int*> x2(x1);
return 0;
}
This still has some weakness. Everybody with an X<T*> can now use
get. But this is so obfuscated by now, that no one is goiing to
realize that. I'd choose a simple public getter.
Example:
template <typename T, typename U>
struct A {
void Print() {}
};
template <>
void A<int, float>::Print() {} // Okay
template <typename T>
void A<T, char>::Print() {} // Will produce error
Question:
I know that you have to define the class template partial specialization in the above code for it to work and I also know from the standard that The members of the class template partial specialization are unrelated to the members of the primary template (§ 14.5.5.3). However, why the difference in syntax between a explication specialization and a partial specialization?
You cannot specialize function templates partially, only fully.
The first instance makes use of the fact that member functions of class templates are themselves function templates, and thus the restriction applies to them.
When you partially-specialize the class template, you have an entirely new class template, which you have to define anew.
template <typename T>
void A<T, char>::Print() {} // Will produce error
You are:
re-defining a function (it has already been defined when declared void Print() {}, you see there are {}.
with a template argument list that doesn't match the declaration: template <typename T, typename U> void Print()
In fact, even if you haven't defined the function when declared it, you will still have an error since your declaration and definition do not match, the compiler will not be able to find a definition for the original template, or a declaration for the specialized template.
A specialized template function for a function that is related to a struct must have a specialized struct as well, This code works:
template <typename T, typename U>
struct A {
void Print() {}
};
template <>
void A<int, float>::Print() {} // Okay
template <typename T>
struct A<T,char>
{
void Print();
};
template <typename T>
void A<T,char>::Print() {}
Because template function has been declared in it's template struct.