473,909 Members | 4,196 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

typeof? How to in C++?

Hi.

Why is this code not working? How can I implement this?
Thanks.
class C
{ private:
struct stat st;
....

public:
typeof(st.st_gi d) gid() const {return st.st_gid;}
....
}
Jun 30 '07 #1
9 18322
On 30 Jun, 20:20, Paulo da Silva <psdasil...@eso tericaX.ptXwrot e:
Hi.

Why is this code not working? How can I implement this?
Thanks.

class C
{ private:
struct stat st;
...

public:
typeof(st.st_gi d) gid() const {return st.st_gid;}
...

}- Hide quoted text -
for gcc use (IIRC) __typeof__ or something like that. There are also
cool hacks for VC7.1 and VC8, that trick the compiler into doing the
same ( probably other compilers too), but officially typeof is missing
from C++. It will be available in the next major c++ version, in form
of decltype and auto AFAIK.

regards
Andy Little
Jun 30 '07 #2
On Jun 30, 12:20 pm, Paulo da Silva <psdasil...@eso tericaX.ptXwrot e:
Hi.

Why is this code not working? How can I implement this?
Thanks.

class C
{ private:
struct stat st;
...

public:
typeof(st.st_gi d) gid() const {return st.st_gid;}
...

}
What purpose could typeof serve that polymorphism doesn't?

Jun 30 '07 #3
Emery escreveu:
On Jun 30, 12:20 pm, Paulo da Silva <psdasil...@eso tericaX.ptXwrot e:
>Hi.

Why is this code not working? How can I implement this?
Thanks.

class C
{ private:
struct stat st;
...

public:
typeof(st.st_gi d) gid() const {return st.st_gid;}
...

}

What purpose could typeof serve that polymorphism doesn't?
The example is clear.
I need to use the stat structure without knowing its members types, i.e.
I am not sure if they are standard or system dependent. typeof fits
exactly my purposes. Somehow it works *only* for static declarations
(for g++ at least). So, I could turn around the problem by declaring a
"dummy" no space taker member:
static struct stat sttype[0];

Then I can write:

typeof(sttype[0].st_gid) gid() const {return st.st_gid;}

Any better solution?
Thanks.
Jul 1 '07 #4
Paulo da Silva wrote:
Emery escreveu:
>On Jun 30, 12:20 pm, Paulo da Silva <psdasil...@eso tericaX.ptXwrot e:
>>Hi.

Why is this code not working? How can I implement this?
Thanks.

class C
{ private:
struct stat st;
...

public:
typeof(st.st_gi d) gid() const {return st.st_gid;}
...

}

What purpose could typeof serve that polymorphism doesn't?
What do you mean?
The example is clear.
I need to use the stat structure without knowing its members types, i.e.
I am not sure if they are standard or system dependent.
They are and are not standard.

They are not standard in that the C++ and C standards does not describe or
reference any struct stat.

They are standard in that POSIX describes them, and POSIX is a standard. The
type of st_gid is defined in <sys/types.h>, which is not a standard C++
header, to gid_t, and is guaranteed to be an arithmetic type.
typeof fits exactly my purposes.
The problem is that it is non-standard, so, like stat, it is off-topic in
this group.
Somehow it works *only* for static
declarations (for g++ at least). So, I could turn around the problem by
declaring a "dummy" no space taker member:
static struct stat sttype[0];

Then I can write:

typeof(sttype[0].st_gid) gid() const {return st.st_gid;}

Any better solution?
a) Read the manual
b) Ask in a group for your operating system
c) Ask in a group for your compiler
d) Wait for the next C++ standard

--
rbh
Jul 1 '07 #5
Robert Bauck Hamar escreveu:
Paulo da Silva wrote:
....
>Any better solution?

a) Read the manual
b) Ask in a group for your operating system
c) Ask in a group for your compiler
d) Wait for the next C++ standard
I am sorry. I didn't know I needed a C++ lawyer degree to post here :-)

Best regards.
Jul 1 '07 #6
Paulo da Silva wrote:
Any better solution?
C++ usually solves this kind of problem with templates. But if you are
trying to retain compatibility with a C library then you are out of
luck. For example:

template<class T>
struct stat
{
T gid;
};

template<class T>
class C
{
private:
stat<Tst;

public:
T gid() const
{
return st.gid;
}
};

Jul 1 '07 #7
Robert Bauck Hamar wrote:
Paulo da Silva wrote:
[snip]
>The example is clear.
I need to use the stat structure without knowing its members types, i.e.
I am not sure if they are standard or system dependent.

They are and are not standard.

They are not standard in that the C++ and C standards does not describe or
reference any struct stat.

They are standard in that POSIX describes them, and POSIX is a standard.
The type of st_gid is defined in <sys/types.h>, which is not a standard
C++ header, to gid_t, and is guaranteed to be an arithmetic type.
>typeof fits exactly my purposes.

The problem is that it is non-standard, so, like stat, it is off-topic in
this group.
Actually, the question whether C++ allows for typing of return values and
variables based upon the type of given expressions, and if so, how one does
this in C++, is topical. It just so happens that the answer is: it's not
(yet) possible. That the answer is short and not helpful does not render
the question off-topic.

Also, extensions of the standard, currently under discussion, are topical
according to the FAQ anyway. Since there are considerations about "auto"
and the likes of it, the problem of the OP seems to be perfectly topical.
That does not change the fact, that as of now, he will have to resort to
compiler specific solutions.
Best

Kai-Uwe Bux
Jul 1 '07 #8
Emery wrote:
Paulo da Silva wrote:
>Any better solution?

C++ usually solves this kind of problem with templates. But if you are
trying to retain compatibility with a C library then you are out of
luck. For example:
The problem is that stat is a struct defined by POSIX, and it is used in an
operating system call. That is, it's sole purpose is to be filled by a
system call, and using other types than the system expects, will fail,
probably with very subtle results. Google for "stat(2)", and you will find
information about it for different systems. So, the use of stat must reatin
compatibility with a C library.
template<class T>
struct stat
{
T gid;
};

template<class T>
class C
{
private:
stat<Tst;

public:
T gid() const
{
return st.gid;
}
};
The real gain had been if something like

decltype(st.st_ gid) gid()

or

auto gid() { return st.st_gid; }

was possible. However it is not. Even if the compiler _knows_ which type
st_gid has, it is not possible to get this type from the compiler. However
this facility is proposed for the next C++ standard:
http://www.open-std.org/jtc1/sc22/wg...2006/n2115.pdf.

--
rbh
Jul 1 '07 #9
On Jul 1, 3:43 am, Robert Bauck Hamar <roberth+n...@i fi.uio.nowrote:
C++ usually solves this kind of problem with templates. But if you are
trying to retain compatibility with a C library then you are out of
luck. For example:

The problem is that stat is a struct defined by POSIX, and it is used in an
operating system call. That is, it's sole purpose is to be filled by a
system call, and using other types than the system expects, will fail,
probably with very subtle results. Google for "stat(2)", and you will find
information about it for different systems. So, the use of stat must reatin
compatibility with a C library.
That's exactly what I was saying...

Jul 2 '07 #10

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

Similar topics

4
299
by: Eric | last post by:
I need to do the following but it doesn't compile if(typeof(listBox1.Items) == typeof(string)){ return; } typeof(listBox1.Items) doesn't work at all. My listBox has 2 types of items in them and I need to know what kind of item a user clicked on so I can use it for drag and drop
4
536
by: ichor | last post by:
hi what is the use of the typeof keyword , and how does it differ from the GetType method. i found the GetType method useful but i fail to understand the use of the typeof method. i have tried out a very simple example which i made up myself. Employee e = new Employee(); ContractEmp ec =new ContractEmp(); MessageBox.Show(typeof(Employee).ToString());
7
10002
by: Mark Miller | last post by:
I am using Reflection.Emit to dynamically build a class. A method of the class to be built requires a Parameter of type "Type". But I don't know how to use Emit to pass a call of "typeof()" to the method. The method could look like this: public void myDynamicMethod(Type type){ //.....do something with the Type passed in....... }
3
950
by: Alberto | last post by:
Can somebody tell me why this typeof doesn't work? foreach (Control myControl in Controls) if (typeof(myControl) == "TextBox") ((TextBox)myControl).Text = string.Empty; Thank you very much
1
1645
by: Brien King | last post by:
Ok, I have three classes (The example here is extremely simplified to illustrate the problem) like this: Public Class A Public Sub DoSomething(ByVal myClass) If TypeOf myClass IS A Then ' ' Never Enters here.... ' End If
11
5633
by: Jason Kendall | last post by:
Why doesn't the new "IsNot" operator work in conjunction with 'Typeof'?
2
2350
by: Andrew Robinson | last post by:
I am guessing there is a simple solution but given a type T, how can I check for nullability? how can I accomplish the following? bool nullable = typeof(int).IsNullable; // false bool nullable = typeof(int?).IsNullable; // true bool nullable = typeof(string).IsNullable; // true Thanks for any help.
4
7120
by: EManning | last post by:
Using A2003. I've got an option group that has a number of check boxes. I have coding to clear the option group if the user wishes to cancel their choice. This coding also clears the rest of the controls on the form. Snippet of the coding: <..snipped..> ElseIf TypeOf oCtrl Is CheckBox Then oCtrl.Value = False <..snipped..> ElseIf TypeOf oCtrl Is OptionGroup Then
20
9333
by: effendi | last post by:
I am testting the following code in firefox function fDHTMLPopulateFields(displayValuesArray, displayOrderArray) { var i, currentElement, displayFieldID, currentChild, nDisplayValues = displayValuesArray.length; for (i=0; i<nDisplayValues; i++) {
20
21808
by: rkk | last post by:
Hi, Is there an equivalent typeof macro/method to determine the type of a variable in runtime & most importantly that works well with most known C compilers? gcc compiler supports typeof() macro, but the same code is not getting compiled in solaris forte compiler and in microsoft VS 2003 compiler. I tried something like below:
0
10035
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
11346
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
10919
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
10538
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...
1
8097
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
6138
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4774
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
4336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3357
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.