473,388 Members | 1,346 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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

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
6 2874
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Vijay singh | last post by:
Hi wonder if anybody can clear by doubt XML file : <score id="1"> <film>A Little Princess</film> <composer>Patrick Doyle</composer>
2
by: Donald Firesmith | last post by:
I am having trouble having Google Adsense code stored in XSL converted properly into HTML. The <> unfortunately become &lt; and &gt; and then no longer work. XSL code is: <script...
20
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src; std::vector<int> dest; ...
13
by: Sherif ElMetainy | last post by:
Hello I was just got VS 2005 preview, and was trying generics. I tried the following code int intArray = new int; IList<int> intList = (IList<int>) intArray; it didn't compile, also the...
14
by: SoilMan | last post by:
Consider the following: class xyz { public: template <typename T> void foo(T x) { cout << "foo<T> " << x << endl; }
4
by: Stuart Moore | last post by:
Hi, I'm quite new to templates and I seem to be getting myself messed up. I want to write a function that takes a map<T, int> and a set<T>, iterates over the set, and increments the corresponding...
2
by: per9000 | last post by:
Hi, *background* I want a class containing an int (a list of sets of integer). This should be hidden for the user and he/she should be able to insert his/her favourite data structure so to be a...
4
by: Grizlyk | last post by:
Hello. Why were base class "typedefs" hidden by template<and explicit usage of them does not work too? Try open only one of the lines in the example below //using Tparent::Tptr; //typedef...
3
by: ajay2552 | last post by:
Hi, I have a query. All html tags start with < and end with >. Suppose i want to display either '<' or '>' or say some text like '<Company>' in html how do i do it? One method is to use &lt,...
45
by: Zytan | last post by:
This returns the following error: "Cannot modify the return value of 'System.Collections.Generic.List<MyStruct>.this' because it is not a variable" and I have no idea why! Do lists return copies...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.