473,789 Members | 3,013 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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*)V ar);
}
void Set(Type* Address) {
Var=(int)Addres s;
}
};

I can declare instances of this class like:

MyClass<int Instance1;
MyClass<shortIn stance2;
MyClass<void Instance3;

etc..

the latter can be written also as:
MyClass<Instanc e3;

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 2064
to**@donotspamm e.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*)V ar);
}
void Set(Type* Address) {
Var=(int)Addres s;
}
};

I can declare instances of this class like:

MyClass<int Instance1;
MyClass<shortIn stance2;
MyClass<void Instance3;

etc..

the latter can be written also as:
MyClass<Instanc e3;

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.publi c.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**@donotspamm e.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*)V ar);
}
void Set(Type* Address) {
Var=(int)Addres s;
}
};

I can declare instances of this class like:

MyClass<int Instance1;
MyClass<shortIn stance2;
MyClass<void Instance3;

etc..

the latter can be written also as:
MyClass<Instanc e3;

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<typena me 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
4783
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> class A { };
3
2137
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 bsort(T * si, T * ei, FUNCTOR cmpfunc) { int k = 0; for (T * i = si; i < ei - 1; i++, k++) for (T * j = si; j < (ei-k-1) ; j++)
6
6669
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 instruction as is written in the paragraph: "statement is the function's body. It can be a single instruction or a block of instructions. In the latter case it must be delimited by curly brackets {}." But I'm not sure if this is true. At least with...
3
2192
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 VecBasis<float, 3> vec3; template<class T, size_t size> class MatBasis { //...
5
2035
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 try to mimic some other examples outther then tailor to my needs. My case is for windows handle like hkey, dll library, file handle, etc. But the code largely uses standard C++. Only the Clean up action, I put windows api call in there. In...
12
2328
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 <iostream> using namespace std; template <class ClassB> class ClassA
11
2353
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 stuck because my compiler does not find a matching function when i do a call. here is my code: // ############## code begins
8
284
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
6649
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
9665
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10408
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9020
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5417
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4092
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.