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

Y

year1943

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

What if I have a template class
template <typename T, typename U> class My ;
and want to have spec-ns like
template <typename T, typename> class 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.
 
G

Gianni Mariani

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

What if I have a template class
template <typename T, typename U> class My ;
and want to have spec-ns like
template <typename T, typename> class 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 U> class My ;
template <typename T> class My<T, int> ;
template <typename T, typename U, typename V>
class My<std::map<T,U>, V> ;
 
Y

year1943

You mean this ?
template <typename T, typename U> class My ;
template <typename T> class 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, int> x3(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.
 
G

Gianni Mariani

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, int> x3(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.
 
Y

year1943

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?..
 
G

Gianni Mariani

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,006
Latest member
LauraSkx64

Latest Threads

Top