469,336 Members | 5,440 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,336 developers. It's quick & easy.

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 17509
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Eric | last post: by
4 posts views Thread by ichor | last post: by
3 posts views Thread by Alberto | last post: by
1 post views Thread by Brien King | last post: by
11 posts views Thread by Jason Kendall | last post: by
2 posts views Thread by Andrew Robinson | last post: by
20 posts views Thread by rkk | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.