473,406 Members | 2,217 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,406 software developers and data experts.

Totally omit default parameter brackets from template?


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<shortInstance2;
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

Aug 24 '06 #1
2 2042
to**@donotspamme.ar wrote:
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<shortInstance2;
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 24 '06 #2
to**@donotspamme.ar wrote:
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<shortInstance2;
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

Aug 24 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Michael Stembera | last post by:
I would like to use default parameters in nested templates but MS VC++ 7.1 chokes on it. Does anyone know how to fix the simple example below or if indeed it is possible? template <int N=7>...
3
by: CoolPint | last post by:
Can anyone explain how I can make the following function accept an default arguement for the last parameter, which should be an optional functor? template <typename T, typename FUNCTOR> void...
6
by: STF | last post by:
While reading the C++ tutorial in this page: http://www.cplusplus.com/doc/tutorial/tut2-2.html I'm astonished to learn that we could omit curly brackets in function declaration for single...
3
by: danilo.horta | last post by:
Hi guys I'm trying to accomplish a slightly difficult task. I think it's more easy to explain trought an unworking code: template<class T, size_t numDim> VecBasis {/*...*/}; typedef...
5
by: asianmuscle | last post by:
I am trying to learn RAII and some template techniques by writer a smarter pointer class to manage the resource management. Since I find that a lot of the resource management is kinda the same, I...
12
by: aaragon | last post by:
Hello all. I have a simple question that seems trivial but I can't make it to work. I have a class that takes as a template argument, another class. The idea is as follows: #include...
11
by: Matthias Pfeifer | last post by:
Hi there, I am trying to declare a function that takes a std::list<parameter. I want this function to have an empty list as a default parameter. It's a template function also. Currently i am...
8
by: William Xu | last post by:
Compiling: template <class T = int> T foo(const T& t) {} int main(int argc, char *argv) {} gcc complains:
2
by: Clyde | last post by:
Hi, what i'm trying to do is: /////////////// Code Start template <class TType, int* p = 0> class Template { public:
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.