473,386 Members | 1,864 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,386 software developers and data experts.

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_gid) gid() const {return st.st_gid;}
....
}
Jun 30 '07 #1
9 18281
On 30 Jun, 20:20, Paulo da Silva <psdasil...@esotericaX.ptXwrote:
Hi.

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

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

public:
typeof(st.st_gid) 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...@esotericaX.ptXwrote:
Hi.

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

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

public:
typeof(st.st_gid) 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...@esotericaX.ptXwrote:
>Hi.

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

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

public:
typeof(st.st_gid) 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...@esotericaX.ptXwrote:
>>Hi.

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

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

public:
typeof(st.st_gid) 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...@ifi.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
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...
4
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...
7
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...
3
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
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 ' '...
11
by: Jason Kendall | last post by:
Why doesn't the new "IsNot" operator work in conjunction with 'Typeof'?
2
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...
4
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...
20
by: effendi | last post by:
I am testting the following code in firefox function fDHTMLPopulateFields(displayValuesArray, displayOrderArray) { var i, currentElement, displayFieldID, currentChild, nDisplayValues =...
20
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()...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...

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.