a typedef question

Discussion in 'C++' started by Fei Liu, Apr 10, 2007.

  1. Fei Liu

    Fei Liu Guest

    Hello, this is a follow-up question I had about typedef usage in Gianni
    Mariani's thread:
    news://213.88.137.53:119/46198ecf$0$13145$

    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
     
    Fei Liu, Apr 10, 2007
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Apr 10, 2007
    #2
    1. Advertising

  3. Fei Liu

    Fei Liu Guest

    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?
     
    Fei Liu, Apr 10, 2007
    #3
  4. 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
     
    Victor Bazarov, Apr 10, 2007
    #4
  5. Fei Liu

    Old Wolf Guest

    On Apr 11, 5:50 am, 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()));
    > >> }

    >
    > >> 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;
    }
     
    Old Wolf, Apr 11, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JustSomeGuy
    Replies:
    1
    Views:
    346
    Mike Wahler
    Sep 20, 2003
  2. James Brown

    typedef question

    James Brown, Nov 16, 2005, in forum: C Programming
    Replies:
    3
    Views:
    464
    James Brown
    Nov 16, 2005
  3. Tony Johansson
    Replies:
    7
    Views:
    362
    Victor Bazarov
    Aug 16, 2005
  4. Alex

    typedef question

    Alex, Nov 28, 2005, in forum: C++
    Replies:
    6
    Views:
    488
    Alex O.
    Dec 2, 2005
  5. oor
    Replies:
    0
    Views:
    1,415
Loading...

Share This Page