473,506 Members | 17,000 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

About Mapping Intergral Constants To Types

Alexandrescu(2000) advance a method about mapping intergral constants
to types like this:
template<int v>
struct Int2Type {
enum { value = v };
};

Why cann't use like this:
template<int v>
struct Int2Type {
};

then we can use it like following:

////////////////////////////////////////////////
template<typename T>
class MyClass {
public:
void dothings() { fun(T()); }
private:
void fun(const Type<true> &a) { cout << "case 1: compiling!\n"; }
void fun(const Type<false> &a) { cout << "case 2: compiling!\n";
Gerald }
void fun(const Type<2> &a) { cout << "case3: compiling!\n"; }
};

int main() {
MyClass<Int2Type<true> > obj1;
obj1.dothings();
// MyClass<Int2Type<false> > obj2;
// obj2.dothings();
// MyClass<Int2Type<2> > obj3;
// obj3.dothings();

return 0;
}

It also works.

another question is that:
Can I use 'class' to replace 'struct' in the definition?
Thank you in advance.

Sep 9 '05 #1
2 1502
mars wrote:
Alexandrescu(2000) advance a method about mapping intergral constants
to types like this:
template<int v>
struct Int2Type {
enum { value = v };
};

Why cann't use like this:
template<int v>
struct Int2Type {
};
You can. I think Andrei's reason for the former version was that he
wanted the value to be accesible.

<code snipped>
another question is that:
Can I use 'class' to replace 'struct' in the definition?
Thank you in advance.


The only difference betwenn class and struct is that in a class all
members are private by default and in a struct they are public by default.

Gabriel
Sep 9 '05 #2
Gabriel wrote:
mars wrote:
Alexandrescu(2000) advance a method about mapping intergral constants
to types like this:
template<int v>
struct Int2Type {
enum { value = v };
};

Why cann't use like this:
template<int v>
struct Int2Type {
};

You can. I think Andrei's reason for the former version was that he
wanted the value to be accesible.

<code snipped>
another question is that:
Can I use 'class' to replace 'struct' in the definition?
Thank you in advance.


The only difference betwenn class and struct is that in a class all
members are private by default and in a struct they are public by default.

Gabriel


Now I found the example:

template<typename T>
class MyClass
{
public:
void dothings()
{
fun(T());
cout << "value is " << T::value << endl; // here it is!
}
private:
void fun(const Int2Type<0> &a) { cout << "case 0: compiling!\n"; }
void fun(const Int2Type<1> &a) { cout << "case 1: compiling!\n"; }
void fun(const Int2Type<2> &a) { cout << "case 2: compiling!\n"; }
};

Ah, and please post code that compiles. you wrote
void fun(const Type<0> &a) ....
instead of
void fun(const Int2Type<0> &a) ....
.. This would help a lot.
Sep 9 '05 #3

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

Similar topics

9
2462
by: Jerry Sievers | last post by:
Fellow Pythonists; I am totally puzzled on the use of slicing on mapping types and especially unsure on use of the Ellipsis... and slicing syntax that has two or more groups seperated by comma....
8
2822
by: john smith | last post by:
I have a question about the best way to use constants in C++. There seem to be variations in the way that they are used, and I am confused as to what I should be using. Basically since the...
9
2748
by: Tim Rentsch | last post by:
I have a question about what ANSI C allows/requires in a particular context related to 'volatile'. Consider the following: volatile int x; int x_remainder_arg( int y ){ return x % y; }
5
2076
by: Zach | last post by:
When it is being said that, "value types are created on the stack or inline as part of an object". If a value type is created in an object, and that object is being called, the value type in that...
65
4134
by: Steven Watanabe | last post by:
I know that the standard idioms for clearing a list are: (1) mylist = (2) del mylist I guess I'm not in the "slicing frame of mind", as someone put it, but can someone explain what the...
6
1537
by: Wescotte | last post by:
I'm having an issue where what should be global variables are not in scope and I'm confused as to why. In Case 1 the variables are not in scope for functions in File2 In Case 2 the varibales are...
37
2178
by: mdh | last post by:
In one of the answers to a K&R exercise, the first couple of lines are: enum loop { NO, YES}; enum loop okloop=YES; I get the first line, but not the second. Sorry about the LOL question. ...
2
1851
by: Alona | last post by:
Hello, Does W3C defines a standard for mapping XML types to C/C++ built-in types and vise versa? Thank you, Alona
3
5830
by: Steve Folly | last post by:
Hi, I had a problem in my code recently which turned out to be the 'the "static initialization order fiasco"' problem (<http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12>) The FAQ...
0
7218
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
7370
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
7478
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...
1
5035
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...
0
4701
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3188
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...
0
1532
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 ...
1
755
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
409
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...

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.