Totally omit default parameter brackets from template?

T

tony

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
 
V

Victor Bazarov

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).

There is no way. A template-id (and that's what "MyClass" is) has
to be followed by the opening angle bracket.
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.

Then post your compiler-specific inquiry to the newsgroup that deals
with that compiler only: microsoft.public.vc.language. We here _do_
care about portability and saying "I don't care" is an insult.

V
 
T

Thomas Tutone

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).

namespace detail {
template<typename T = void>
class Foo {};
}

typedef detail::Foo<> Foo;

Best regards,

Tom
 

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,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top