 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Thu Aug 24, 2006 11:13 pm Post subject: Totally omit default parameter brackets from template? |
|
|
{ this has been asked and answered in c.l.c++ and m.p.v.l, could
we perhaps avoid giving the same answers, maybe? thanks. -mod }
Hello!
I have this template class with a default type (please do not give too
much importance to the actual code, it's just an example, it's not a
useful implementation of anything nor a good example of use of void*):
template <typename Type = void>
class MyClass {
int Var;
Type* Get() {
return((Type*)Var);
}
void Set(Type* Address) {
Var=(int)Address;
}
};
I can declare instances of this class like:
MyClass<int> Instance1;
MyClass<short> Instance2;
MyClass<void> Instance3;
etc..
the latter can be written also as:
MyClass<> Instance3;
and here's finally the question: I'd like to use also this default form:
MyClass Instance3;
which of course will be perfectly equivalent to MyClass<> and MyClass<void>
How do I do it? I tried some namespace trick, but not successfully (which
doesn't mean it's not the right way to go, but just that I'm too lame to
successfully bring it to life).
Please note I am using VisualC++ 8.0 (a.k.a. 2005) targeting native x86
code on 32bit Windows and I do NOT care about portability for the specific
case, so any trick that would work with this compiler would be fine++ for
me.
Thank you very much,
Tony
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Fri Aug 25, 2006 8:03 pm Post subject: Re: Totally omit default parameter brackets from template? |
|
|
In article <44edd05a$1$30237$4fafbaef (AT) reader1 (DOT) news.tin.it>,
tony (AT) donotspamme (DOT) ar writes
| Quote: | { this has been asked and answered in c.l.c++ and m.p.v.l, could
we perhaps avoid giving the same answers, maybe? thanks. -mod }
|
The only way many of us can do that is not to give any answer. I
hardly
ever read c.l.c++ and never read m.p.v.l. I realise the request was
well-intentioned but I do not think it helps.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' and "You Can Program in C++"
see http://www.spellen.org/youcandoit
For project ideas and contributions:
http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Greg Herlihy Guest
|
Posted: Sat Aug 26, 2006 9:10 am Post subject: Re: Totally omit default parameter brackets from template? |
|
|
tony (AT) donotspamme (DOT) ar wrote:
| Quote: | { this has been asked and answered in c.l.c++ and m.p.v.l, could
we perhaps avoid giving the same answers, maybe? thanks. -mod }
Hello!
I have this template class with a default type (please do not give too
much importance to the actual code, it's just an example, it's not a
useful implementation of anything nor a good example of use of void*):
template <typename Type = void
class MyClass {
....
}
};
.....
MyClass<void> Instance3;
the latter can be written also as:
MyClass<> Instance3;
and here's finally the question: I'd like to use also this default form:
MyClass Instance3;
....
How do I do it?
|
But the name "MyClass" is already in use: "MyClass" is the name of the
class template from which MyClass<void> is instantiated. So the name
"MyClass" is not available (at least in the desired scope) to be used
as a typedef name for the MyClass<void> template class. And if C++ did
not impose the restriction, then the "MyClass" name would be
potentially ambiguous. And if the C++ compiler can have trouble telling
the MyClass class template from the MyClass template class - imagine
how well other programmers who may have to work on this code in the
future - will cope with these names.
So instead of forging ahead with the effort to create ambiguous typedef
names, a more simple and surefire solution would simply be to pick some
other name for the MyClass<void> typedef - that is, some other name
that is not already in use.
Greg
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|