473,468 Members | 1,358 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

a typedef question

Hello, this is a follow-up question I had about typedef usage in Gianni
Mariani's thread:
news://213.88.137.53:119/46***********************@per-qv1-newsreader-01.iinet.net.au

int foo(int A, int * pA){ return 0; }
int main(){
typedef int * pi;
typedef int ii;
int a = sizeof(foo(ii(), pi()));
}
I understand the function foo never actually gets called, but what
causes the compiler to ignore this usage (without any actual storage
unit or variable declaration)? Normally one cannot call a function like
this. Also I didn't find any reference that sizeof can be used to take
the size of a function return type during compile time. I know what this
code is doing and it makes sense to me. But it seems like I can't find
any reference that says this is correct and proper use of C++ syntax.

This is the first time this kind of sizeof and typedef usage get my
attention, where I can read/learn about this typedef trick?

Thanks,

Fei
Apr 10 '07 #1
4 1702
Fei Liu wrote:
[..]
int foo(int A, int * pA){ return 0; }
int main(){
typedef int * pi;
typedef int ii;
int a = sizeof(foo(ii(), pi()));
}
I understand the function foo never actually gets called, but what
causes the compiler to ignore this usage (without any actual storage
unit or variable declaration)?
5.3.3/1 Sizeof
"... The operand is either an expression *which is not evaluated* ..."
(emphasis mine).
Normally one cannot call a function
like this.
Not sure what you meant by "normally". And how is it "like this"?
Temporaries of type 'ii' and 'pi' are constructed and passed to the
function. Just remove 'sizeof' and see what happens.
Also I didn't find any reference that sizeof can be used
to take the size of a function return type during compile time.
Again, 5.3.3/1. What of "an expression" do you not get?
I
know what this code is doing and it makes sense to me. But it seems
like I can't find any reference that says this is correct and proper
use of C++ syntax.
This is the first time this kind of sizeof and typedef usage get my
attention, where I can read/learn about this typedef trick?
WHAT typedef trick? You mean you never seen something like "blah()"
for 'blah' that is a type?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 10 '07 #2
Victor Bazarov wrote:
Fei Liu wrote:
>[..]
int foo(int A, int * pA){ return 0; }
int main(){
typedef int * pi;
typedef int ii;
int a = sizeof(foo(ii(), pi()));
}
I understand the function foo never actually gets called, but what
causes the compiler to ignore this usage (without any actual storage
unit or variable declaration)?

5.3.3/1 Sizeof
"... The operand is either an expression *which is not evaluated* ..."
(emphasis mine).
>Normally one cannot call a function
like this.

Not sure what you meant by "normally". And how is it "like this"?
Temporaries of type 'ii' and 'pi' are constructed and passed to the
function. Just remove 'sizeof' and see what happens.
>Also I didn't find any reference that sizeof can be used
to take the size of a function return type during compile time.

Again, 5.3.3/1. What of "an expression" do you not get?
>I
know what this code is doing and it makes sense to me. But it seems
like I can't find any reference that says this is correct and proper
use of C++ syntax.
This is the first time this kind of sizeof and typedef usage get my
attention, where I can read/learn about this typedef trick?

WHAT typedef trick? You mean you never seen something like "blah()"
for 'blah' that is a type?

V
Thanks for the pointer to 5.3.3/1 in the standard. But seriously, did
you forget to take your medicine?
Apr 10 '07 #3
Fei Liu wrote:
Victor Bazarov wrote:
>Fei Liu wrote:
>>[..]
int foo(int A, int * pA){ return 0; }
int main(){
typedef int * pi;
typedef int ii;
int a = sizeof(foo(ii(), pi()));
}
I understand the function foo never actually gets called, but what
causes the compiler to ignore this usage (without any actual storage
unit or variable declaration)?

5.3.3/1 Sizeof
"... The operand is either an expression *which is not evaluated*
..." (emphasis mine).
>>Normally one cannot call a function
like this.

Not sure what you meant by "normally". And how is it "like this"?
Temporaries of type 'ii' and 'pi' are constructed and passed to the
function. Just remove 'sizeof' and see what happens.
>>Also I didn't find any reference that sizeof can be used
to take the size of a function return type during compile time.

Again, 5.3.3/1. What of "an expression" do you not get?
>>I
know what this code is doing and it makes sense to me. But it seems
like I can't find any reference that says this is correct and proper
use of C++ syntax.
This is the first time this kind of sizeof and typedef usage get my
attention, where I can read/learn about this typedef trick?

WHAT typedef trick? You mean you never seen something like "blah()"
for 'blah' that is a type?

V

Thanks for the pointer to 5.3.3/1 in the standard. But seriously, did
you forget to take your medicine?
What makes you think there is some medicine I am usually taking?
Are you a doctor?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 10 '07 #4
On Apr 11, 5:50 am, Fei Liu <fei...@aepnetworks.comwrote:
Victor Bazarov wrote:
Fei Liu wrote:
[..]
int foo(int A, int * pA){ return 0; }
int main(){
typedef int * pi;
typedef int ii;
int a = sizeof(foo(ii(), pi()));
}
This is the first time this kind of sizeof and typedef usage get my
attention, where I can read/learn about this typedef trick?
WHAT typedef trick? You mean you never seen something like "blah()"
for 'blah' that is a type?

Thanks for the pointer to 5.3.3/1 in the standard. But seriously, did
you forget to take your medicine?
FWIW, I too have no idea what you meant by "typedef trick". The two
typedefs in the original code are pretty basic, it's hard to imagine
a simpler typedef. I also have no idea what you meant by:
I understand the function foo never actually gets called, but what
causes the compiler to ignore this usage (without any actual storage
unit or variable declaration)?
unless perhaps, as Victor hypothesized, you aren't familiar
with the syntax T() for creating a temporary of type T, or
if you aren't aware that the sizeof operator can be applied
to any expression.

Can you explain?

#include <stdio.h>

int foo(int A, int * pA){ return 0; }
int main(){
typedef int * pi;
typedef int ii;
int a = sizeof foo(ii(), pi());
int b = foo(ii(), pi());
printf("a = %d, b = %d\n", a, b); /* probably 4, 0 */
return 0;
}

Apr 11 '07 #5

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

Similar topics

3
by: Generic Usenet Account | last post by:
This is a two-part question. (1) I have implemented a "Datastructure Registry" template class. I am getting no compiler warnings with older compilers, but newer compilers are generating the...
7
by: Tony Johansson | last post by:
Hello Experts! I have the following Array template class see below. I execute these three statements statement 1: Array<int> x(5); statement 2: cin >>x; statement 3: Array<int>::element_type ...
30
by: stephen henry | last post by:
Hi all, I have a question that I'm having difficulty answering. If I have a struct: typedef struct my_struct_tag{ struct my_other_struct *other; } my_struct_tag
6
by: Alex | last post by:
Hello people, I am getting errors from VS2003 when working with typedef'ed types. For example, assume that I have a type T, defined in a 3rd party include file based on some condition #if...
12
by: Thomas Carter | last post by:
Imagine that there is some include file f.h that contains the following line: typedef unsigned int ui32 ; My question is: If I have a C source file F.c that includes f.h, is it possible for...
7
by: Michael B Allen | last post by:
I have a forward reference like: struct foo; int some_fn(struct foo *param); Because the parameter is a pointer the compiler is satisfied. But now I wan to change 'struct foo' to a...
12
by: Googy | last post by:
Hi!! Can any one explain me the meaning of following notations clearly : 1. typedef char(*(*frpapfrc()))(); frpapfrc f; 2. typedef int (*(arr2d_ptr)()); arr2d_ptr p; 3. typedef int...
16
by: mdh | last post by:
A quick ? :-) question about Typedefs. There is a very brief discussion about this in K&R ( p146). Googling this group, there is a surprising dearth of questions about these. From one of the...
7
by: MJ_India | last post by:
Style 1: struct my_struct { ... }; typedef my_struct my_struct_t; Style 2: typedef struct my_struct {
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.