473,695 Members | 2,425 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

About Mapping Intergral Constants To Types

Alexandrescu(20 00) 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<typena me 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<Int2Typ e<true> > obj1;
obj1.dothings() ;
// MyClass<Int2Typ e<false> > obj2;
// obj2.dothings() ;
// MyClass<Int2Typ e<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 1518
mars wrote:
Alexandrescu(20 00) 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(20 00) 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<typena me 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
2471
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. I am referring to (from the manual); Slice objects Slice objects are used to represent slices when extended
8
2830
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 constant is used in only one class, should I try to scope it to just that class? to the file? what are the thoughts out there. 1.) I have seen people declare in their .h file
9
2760
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
2097
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 object, is still created on the stack, I would say, so I don't understand this inline business. Apart from the fact that it is my understanding that "inline" as it exists in C++ doesn't exist in C#. Could someone please shed some light on this...
65
4202
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 difference is between these and: (3) mylist =
6
1545
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 in scope for access in File1's functions AFTER the include In Case 3 the varibales are in scope for functionsin File2 Why does case 1 not work but case 3 does? Does case 2 the proper method
37
2210
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. Thanks in advance
2
1855
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
5851
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 section describes a solution using methods returning references to static objects. But consider:
0
9116
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
8985
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7661
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...
1
6492
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5839
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4341
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
4579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3007
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
2
2273
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.