Does the typedef definition matter inside a extern "C" declration

Discussion in 'C++' started by parag_paul@hotmail.com, Jun 11, 2008.

  1. Guest

    Does the following typedef differ from a typedef inside a extern "C"


    #if defined(__cplusplus)
    extern "C" {
    #endif


    typedef int (*fun_point)(int);

    #if defined(__cplusplus)
    }
    #endif



    then a plain typedef
     
    , Jun 11, 2008
    #1
    1. Advertising

  2. Ian Collins Guest

    wrote:
    > Does the following typedef differ from a typedef inside a extern "C"
    >
    >
    > #if defined(__cplusplus)
    > extern "C" {
    > #endif
    >
    >
    > typedef int (*fun_point)(int);
    >
    > #if defined(__cplusplus)
    > }
    > #endif
    >

    Yes, the above is idiomatic for headers files shared between C and C++.

    >
    > then a plain typedef


    I'm not sure what you mean by this. In the example above, fun_point is
    a pointer to a C linkage function.

    If the typedef wasn't wrapped with extern "C", fun_point would be a
    pointer to a C++ free function.

    --
    Ian Collins.
     
    Ian Collins, Jun 11, 2008
    #2
    1. Advertising

  3. Guest

    On Jun 11, 2:30 pm, Ian Collins <> wrote:
    > wrote:
    > > Does the following typedef differ from a typedef inside a extern "C"

    >
    > > #if defined(__cplusplus)
    > > extern "C" {
    > > #endif

    >
    > > typedef (*fun_point)(int);

    >
    > > #if defined(__cplusplus)
    > > }
    > > #endif

    >
    > Yes, the above is idiomatic for headers files shared between C and C++.
    >
    >
    >
    > > then a plain typedef

    >
    > I'm not sure what you mean by this.  In the example above, fun_point is
    > a pointer to a C linkage function.
    >
    > If the typedef wasn't wrapped with extern "C", fun_point would be a
    > pointer to a C++ free function.
    >
    > --
    > Ian Collin


    since I used fun_point in the prototype and in teh definition I used
    the int (*fun_point)(int)
    and it was complaining of anachronism
     
    , Jun 11, 2008
    #3
  4. James Kanze Guest

    On Jun 11, 4:23 pm, "" <>
    wrote:
    > On Jun 11, 2:30 pm, Ian Collins <> wrote:
    > > wrote:
    > > > Does the following typedef differ from a typedef inside a
    > > > extern "C"


    > > > #if defined(__cplusplus)
    > > > extern "C" {
    > > > #endif


    > > > typedef (*fun_point)(int);


    > > > #if defined(__cplusplus)
    > > > }
    > > > #endif


    > > Yes, the above is idiomatic for headers files shared between
    > > C and C++.


    > > > then a plain typedef


    > > I'm not sure what you mean by this. In the example above, fun_point is
    > > a pointer to a C linkage function.


    > > If the typedef wasn't wrapped with extern "C", fun_point would be a
    > > pointer to a C++ free function.


    > since I used fun_point in the prototype and in teh definition I used
    > the int (*fun_point)(int)
    > and it was complaining of anachronism


    To clarify what Ian said, given:

    extern "C" {
    typedef void (*fp1)( int ) ;
    }
    typedef void (*fp2)( int ) ;

    fp1 and fp2 are two different, unrelated types. You cannot use
    one for the other; if you do, the compiler is required to emit a
    diagnostic. (Some compilers have errors in this regard.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jun 12, 2008
    #4
    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. class declration

    , Jul 25, 2004, in forum: C++
    Replies:
    2
    Views:
    390
  2. Thomas Matthews
    Replies:
    5
    Views:
    2,440
    tom_usenet
    Aug 2, 2004
  3. Jianli Shen
    Replies:
    1
    Views:
    594
    Victor Bazarov
    Mar 13, 2005
  4. Henning Hasemann
    Replies:
    3
    Views:
    369
    Henning Hasemann
    Jun 14, 2006
  5. Andre
    Replies:
    5
    Views:
    542
    Keith Thompson
    Jul 17, 2012
Loading...

Share This Page