By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,127 Members | 1,237 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,127 IT Pros & Developers. It's quick & easy.

Template partial specializations like <A, B> specialized for <A, int>- possible?

P: n/a
For
template <typename Tclass My ;
I can define partial spec-ns somewhat like
template <typename Tclass My<T*; or
template <typename Tclass My<Another<T ;
And full spec-n, say
template <class My<int;

What if I have a template class
template <typename T, typename Uclass My ;
and want to have spec-ns like
template <typename T, typenameclass My<T, int;
when spec-n is not "full", but one of template-parameters is "not
used"?

Is it possible? If it is, what is right syntax. For my tries GCC says
something like

60-tpl_partial.cpp:49: error: template parameters not used in partial
specialization:
60-tpl_partial.cpp:49: error: '<template-parameter-1-2>'

If it is not possible, what are reasons to prohibit that?
Thanx.

Oct 10 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
ye******@front.ru wrote:
For
template <typename Tclass My ;
I can define partial spec-ns somewhat like
template <typename Tclass My<T*; or
template <typename Tclass My<Another<T ;
And full spec-n, say
template <class My<int;

What if I have a template class
template <typename T, typename Uclass My ;
and want to have spec-ns like
template <typename T, typenameclass My<T, int;
when spec-n is not "full", but one of template-parameters is "not
used"?

Is it possible? If it is, what is right syntax.
You mean this ?

template <typename T, typename Uclass My ;
template <typename Tclass My<T, int;
template <typename T, typename U, typename V>
class My<std::map<T,U>, V;
Oct 10 '07 #2

P: n/a
On 11 , 00:42, Gianni Mariani <gi4nos...@marian.wswrote:
You mean this ?
template <typename T, typename Uclass My ;
template <typename Tclass My<T, int;
template <typename T, typename U, typename V>
class My<std::map<T,U>, V;
Thanks, seems it's OK. Perhaps I messed and thought number or tpl-args
should match...
Initially I was interested in spec-n of not whole class, but just a
member-function of it. Still havn't found right way. Here sample code:

#include <iostream>

template <typename A, typename B>
class Test2 {
public:

Test2(A a, B b)
: _a(a), _b(b)
{
return ;
}

void print(void) ;

private:
A _a ;
B _b ;
} ;

template <typename A, typename B>
void Test2<A, B>::print(void) {
std::cout << "Generic<A,B>: " << _a << ", " << _b << std::endl ;
return ;
}

// template <typename A, typename>
template <typename A>
void Test2<A, int>::print(void) {
std::cout << "Spec<A, int>: " << _a << ", " << _b << std::endl ;
return ;
}

int main() {
Test2<int, const char*x1(37, "const char* sample") ;
Test2<double, intx3(370.37768, 2009) ;
x1.print() ;
x3.print() ;
return 0 ;
}

GCC tells:
60-tpl_partial.cpp:32: error: invalid use of undefined type 'class
Test2<A, int>'
60-tpl_partial.cpp:6: error: declaration of 'class Test2<A, int>'
60-tpl_partial.cpp:32: error: template definition of non-template
'void Test2<A, int>::print()'

for both variants
template <typename A, typename>
template <typename A>
What yet is missed?

Thanx.

Oct 10 '07 #3

P: n/a
ye******@front.ru wrote:
On 11 , 00:42, Gianni Mariani <gi4nos...@marian.wswrote:
>You mean this ?
template <typename T, typename Uclass My ;
template <typename Tclass My<T, int;
template <typename T, typename U, typename V>
class My<std::map<T,U>, V;

Thanks, seems it's OK. Perhaps I messed and thought number or tpl-args
should match...
Initially I was interested in spec-n of not whole class, but just a
member-function of it. Still havn't found right way. Here sample code:
....

You can only partially specialize the entire class, not components of a
class. This is how it can be done.

#include <iostream>

template <typename A, typename B>
class Test2_Base {
public:

Test2_Base(A a, B b)
: _a(a), _b(b)
{
return ;
}

void print(void) ;

protected:
A _a ;
B _b ;
} ;
template <typename A, typename B>
class Test2 : public Test2_Base<A,B {
public:

Test2(A a, B b)
: Test2_Base<A,B>( a, b )
{
return ;
}
} ;

template <typename A>
class Test2<A, int: public Test2_Base<A,int {
public:

Test2(A a, int b)
: Test2_Base<A,int>( a, b )
{
return ;
}

void print(void) ;

} ;

template <typename A, typename B>
void Test2_Base<A, B>::print(void) {
std::cout << "Generic<A,B>: " << _a << ", " << _b << std::endl ;
return ;
}

// template <typename A, typename>
template <typename A>
void Test2<A, int>::print(void) {
std::cout << "Spec<A, int>: " << this->_a << ", " << this->_b <<
std::endl ;

return ;
}

int main() {
Test2<int, const char*x1(37, "const char* sample") ;
Test2<double, intx3(370.37768, 2009) ;
x1.print() ;
x3.print() ;
return 0 ;
}

.... however, depending on what you're really trying to do, it may be
better to use a traits class specialization.
Oct 10 '07 #4

P: n/a
On 11 , 02:03, Gianni Mariani <gi4nos...@marian.wswrote:
You can only partially specialize the entire class, not components of a
class. This is how it can be done.
..............
... however, depending on what you're really trying to do, it may be
better to use a traits class specialization.
I've got the rule and the idea (and traits too), thank you very much.

The last (not practical) question may be _why_ is it so...
For
template <typename T>
MyClass<T*>::method()
partial spec-n of member w/o spec-n of whole class can be problematic
and vogue, so as for generic<some_type*T is some_type* while for
member spec-n<T*T is just some_type...
But for
template <typename T1, typename T2>
MyClass<T1, concrete_type>::method()
are there some difficulties that let us not to use separate member
template partial specializations?..

Oct 10 '07 #5

P: n/a
ye******@front.ru wrote:
On 11 , 02:03, Gianni Mariani <gi4nos...@marian.wswrote:
>You can only partially specialize the entire class, not components of a
class. This is how it can be done.
.............
>... however, depending on what you're really trying to do, it may be
better to use a traits class specialization.

I've got the rule and the idea (and traits too), thank you very much.

The last (not practical) question may be _why_ is it so...
For
template <typename T>
MyClass<T*>::method()
partial spec-n of member w/o spec-n of whole class can be problematic
and vogue, so as for generic<some_type*T is some_type* while for
member spec-n<T*T is just some_type...
But for
template <typename T1, typename T2>
MyClass<T1, concrete_type>::method()
are there some difficulties that let us not to use separate member
template partial specializations?..
I don't know other than "it is". Maybe someone else can shed light on
the issue.
Oct 10 '07 #6

P: n/a
On 11 , 03:10, Gianni Mariani <gi4nos...@marian.wswrote:
I don't know other than "it is". Maybe someone else can shed light on
the issue.
Thanks a lot, anyway.
I found quite similar topic, yesterday it had no answers, now it got
some - in neibour c++.moderated:
http://groups.google.ru/group/comp.l...035ce2081b3379
One decision looks strange as for me, two others are close to
"traits" (realized as "helpers").

Oct 11 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.