f



workaround to C "typedef struct name *name;" C vs C++

Hi All,

This is valid code in C because "struct name" and "name" are distinct

{ the poster refers to this part of the subj line:

   typedef struct name *name;

  -mod }

types, but not in C++ where this produces a compiler error. I have a
third party C library that uses this extensively in header files and I
need to use this in C++ code.

Is there a way around this?

Thanks,
Brian


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Brian
8/25/2011 12:30:32 PM
comp.lang.c++.moderated 10738 articles. 1 followers. allnor (8510) is leader. Post Follow

6 Replies
684 Views

Similar Articles

[PageSpeed] 36

Am 25.08.2011 14:30, schrieb Brian L:
> Hi All,
>
> This is valid code in C because "struct name" and "name" are distinct
>
> { the poster refers to this part of the subj line:
>
>     typedef struct name *name;
>
>    -mod }
>
> types, but not in C++ where this produces a compiler error. I have a
> third party C library that uses this extensively in header files and I
> need to use this in C++ code.
>
> Is there a way around this?

What do you mean with a way around? You did not really explain what the
actual reason for this construction is. What do you want to realize?

The C++ standard is clear that this typedef declaration is invalid in a
non-class scope, because the second 'name' does refer to something
different than the second one. I can define a construction that works,
but I don't know whether this will help you, you just need to move the
typedef declaration in a class scope:

struct C {
   typedef struct name *name;
};

This works, because of 7.1.3 [dcl.typedef] p4:

"In a given class scope, a typedef specifier can be used to redefine any
class-name declared in that scope that is not also a typedef-name to
refer to the type to which it already refers."

Within C the first part 'struct name' refers to a yet undeclared class
type name in the global scope and so we define C::name as a pointer to
::name. But this means that always need to refer to C::name instead of
name as in your OP.

HTH & Greetings from Bremen,

- Daniel Kr�gler


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
ISO
8/25/2011 10:58:34 PM
On Aug 25, 7:30 am, Brian L <blev.no.s...@gmail.com> wrote:
> This is valid code in C because "struct name" and "name" are distinct
>
> { the poster refers to this part of the subj line:
>
>    typedef struct name *name;
>
>   -mod }
>
> types, but not in C++ where this produces a compiler error. I have a
> third party C library that uses this extensively in header files and I
> need to use this in C++ code.
>
> Is there a way around this?

Try using extern "C" to specify C linkage - you can find examples
online

// in header
extern "C" {
int my_c_function1();
char* my_c_function2(int n);
}

// in C++ source file
extern "C" {
#include <my_c_header.h>
}


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
John
8/27/2011 2:20:26 AM
{ quoted c.l.c++.m server banner removed. -mod }

On Aug 25, 5:30 am, Brian L <blev.no.s...@gmail.com> wrote:
> Hi All,
>
> This is valid code in C because "struct name" and "name" are distinct
>
> { the poster refers to this part of the subj line:
>
>    typedef struct name *name;
>
>   -mod }
>
> types, but not in C++ where this produces a compiler error. I have a
> third party C library that uses this extensively in header files and I
> need to use this in C++ code.
>
> Is there a way around this?
>
> Thanks,
> Brian

If you're asking for how to have struct tags peacefully coexist in C
and C++, Dan Saks has a thorough discussion of the issue in this
article: http://www.eetimes.com/discussion/programming-pointers/4024450/Tag-vs-Type-Names

If you're asking how to make use of your third-party C library in your
C++ programs, you'll have to surround places you #include them with
extern "C" {}: http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html#faq-32.3


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Gnawme
8/27/2011 12:52:23 PM
On 8/26/11 10:20 PM, John Shaw wrote:
> On Aug 25, 7:30 am, Brian L<blev.no.s...@gmail.com>  wrote:
>> This is valid code in C because "struct name" and "name" are distinct
>>
>> { the poster refers to this part of the subj line:
>>
>>     typedef struct name *name;
>>
>>    -mod }
>>
>> types, but not in C++ where this produces a compiler error. I have a
>> third party C library that uses this extensively in header files and I
>> need to use this in C++ code.
>>
>> Is there a way around this?
>
> Try using extern "C" to specify C linkage - you can find examples
> online
>

I don't think extern "C" will help here. extern "C" changes the linkage 
rules (things like parameter passing rules and name mangling), they 
DON'T change the language syntax of what is inside them.

If the header uses the line

typedef struct name *name

then that header is NOT C++ compatible, and the library will need to 
have a opaque C wrapper around it to expose a modified version of the 
API that is C++ compatible.

Because of the opaque nature needed, it may well need to use things like 
PIMPL to hide the details that C++ can't see due to the incompatibility, 
but if the C++ only needs to use the pointer version of the name, you 
may be able to get away with just duplicating the api with a definition of

typedef struct name_tag *name;

This does mean that "struct name" needs only be seen by C code, and 
name_tag should not be used in any context that needs more than an 
opaque pointer.


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Richard
8/27/2011 12:54:39 PM
Am 27.08.2011 04:20, schrieb John Shaw:
> On Aug 25, 7:30 am, Brian L<blev.no.s...@gmail.com>  wrote:
>> This is valid code in C because "struct name" and "name" are distinct
>>
>> { the poster refers to this part of the subj line:
>>
>>     typedef struct name *name;
>>
>>    -mod }
>>
>> types, but not in C++ where this produces a compiler error. I have a
>> third party C library that uses this extensively in header files and I
>> need to use this in C++ code.
>>
>> Is there a way around this?
>
> Try using extern "C" to specify C linkage - you can find examples
> online
>
> // in header
> extern "C" {
> int my_c_function1();
> char* my_c_function2(int n);
> }
>
> // in C++ source file
> extern "C" {
> #include<my_c_header.h>
> }

How should that solve the problem described by the OP? The compiler he 
uses will still be a C++ compiler and will still be required to reject 
the declaration, thus this won't fix the problem.

HTH & Greetings from Bremen,

- Daniel Kr�gler



-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
ISO
8/27/2011 12:56:17 PM
Daniel Krügler wrote:

> Am 25.08.2011 14:30, schrieb Brian L:
....
> struct C {
>    typedef struct name *name;
> };
>
> This works, because of 7.1.3 [dcl.typedef] p4:
>
> "In a given class scope, a typedef specifier can be used to redefine any
> class-name declared in that scope that is not also a typedef-name to
> refer to the type to which it already refers."
>

Well, it would work independently of 7.1.3. You are not redefining a class-
name declared in the scope of class "C". The class-name is declared
(invisibly, c.f. note at 3.3p4) in the namespace scope surrounding class C
(see 3.3.2p6 second bullet). The class is a member of that namespace.

And you are not referring to the same type both times.

> Within C the first part 'struct name' refers to a yet undeclared class
> type name in the global scope and so we define C::name as a pointer to
> ::name. But this means that always need to refer to C::name instead of
> name as in your OP.
>

This is correct, but I wonder whether this is not a violation of the name-
must-have-same-meaning rule. Because the use of "name" in "struct name"
refers to the class name in the surrounding namespace, but if you would use
"struct name" again in the class with the complete scope of "C" available,
it would not anymore refer to that namespace member name, but would 
refer to
the typedef-name. That would make the program ill-formed (by 7.1.6.3p2), 
but
3.3.7p1.2 is violated regardless.

For a pedantic interpretation of 3.3.7.p1.2 and 7.1.3, we can argue even 
the
following runs afoul of this rule too:

   struct A {
     struct B { };
     typedef struct B B;
   };

That's because the model by which the typedef name is introduced inspite of
the already existing class name is redefinition of the name. Not
overloading/hiding and not different identifier namespaces like in C. So in
the complete scope of "A", a use like "struct B" can not refer to the
declaration of the class-name introduced by "struct B { };", because that
name was redefined to refer to the typedef-name introduced by the typedef-
declaration. Note that in this case, the program would not be affected by
7.1.6.3p2, because of the rule added to the FDIS at 7.1.3p5. But 3.3.7p1.2
will draw it ill-formed; NDR. But I believe the intention is for this
example code to be valid, though.


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Johannes
8/28/2011 1:04:18 AM
Reply:

Similar Artilces:

Is C# really "better" than C++ or C++0x? How about objective-C?
Microsoft thinks C++ is obsolete and C# is "the future". Apple thinks the same for Objective-C. IMO this is all nonsense. What do you think about this? How "better" really they are? And what about C++0x? On Sun, 17 Apr 2011 16:58:37 +0200, "A" <a@a.a> wrote: >Microsoft thinks C++ is obsolete and C# is "the future". Apple thinks the >same for Objective-C. > >IMO this is all nonsense. I agree. > >What do you think about this? How "better" really they are? And what about >C++0x? > Companies like Microsoft ...

What does "Standard C", "K&R C" , "ANSI C" mean?
I am just wondering what the following terms usually mean: 1) "Standard C" 2) "K&R C" 3) "ANSI C" I am pretty sure "ANSI C" usually refers to the C89 standard, but what about the other two? What is the "saying" for C99 standard? Thank you On 17 Jan 2005 21:26:42 -0800, "Luke Wu" <LookSkywalker@gmail.com> wrote in comp.lang.c: > I am just wondering what the following terms usually mean: > > 1) "Standard C" The current version of the C language standard. This is now known as "ISO/IEC 9899:19...

Is this 'C'/C++? struct device dev = { .name = "dev0", };
Is this really C/C++? struct device { char const* name; }; device dev = { .name = "dev", }; The Linux sources are littered with this kind of structure initialisation; is it legal C/C++? I've just tried 3 different C/C++ compilers and none of them accept this kind of member initialisation syntax .name = "dev" Is this syntax GNU 'C'-specific? Tim Clacy wrote: > Is this really C/C++? > > struct device > { > char const* name; > }; > > device dev = > { > .name = "dev", > }; > > The Linux ...

ascii ?c "c"[0] "c".ord.chr question
I was curious why the prefix ? for ascii code was removed in 1.9.x? ~ On Dec 3, 2010, at 12:39 , Stu wrote: > I was curious why the prefix ? for ascii code was removed in 1.9.x? It wasn't removed, it just changed what it returns to work with = multibyte chars: > multiruby -e 'p ?c' >=20 > VERSION =3D 1.8.6-p399 > CMD =3D ~/.multiruby/install/1.8.6-p399/bin/ruby -e p ?c >=20 > 99 >=20 > RESULT =3D 0 >=20 > VERSION =3D 1.8.7-p302 > CMD =3D ~/.multiruby/install/1.8.7-p302/bin/ruby -e p ?c >=20 > 99 >=...

jython and C-c C-c
How do I get C-c C-c to work with jython? I have the jpython command set to jython, and I can start the interpreter with C-C ! and then use C-c C-c, but this is rather frustrating. If I try to use C-c C-c without first starting the interpreter in another window I get wrong type argument: sequencep, jpython Thanks, Dave Cook ...

Better C/C++ Than C/C++?
I am looking for a good systems programming language that can be used instead of C/C++. My qualifications for the language are: * mature compiler(s) that produce native-code binaries (for Windows) * open source - preferable, but not 100% necessary Thanks, Kevin "Kevin Albrecht" <kevin@albrecht.net> writes: > I am looking for a good systems programming language > that can be used instead of C/C++. My qualifications > for the language are: > > * mature compiler(s) that produce native-code > binaries (for Windows) Ocaml, D, cyclone, Eiffel, Beta > * open source - preferable, but not 100% necessary what's wrong with using C? Friedrich > what's wrong with using C? Too low level, no built-in garbage collection. -- Kevin "Kevin Albrecht" <kevin@albrecht.net> writes: >> what's wrong with using C? > > Too low level, Well the Smalltalk people say, have a small language and a big library. I assume C's library status beyond that of Smalltalk. > no built-in garbage collection. Use the Boehm Weisser GC. Friedrich "Kevin Albrecht" <kevin@albrecht.net> wrote in message news:<2TiYb.237892$rj7.1641209@twister.tampabay.rr.com>... > I am looking for a good systems programming language > that can be used instead of C/C++. My qualifications > for the language are: > > * mature compiler(s) that produce native-code > binaries (for Windows) > ...

"C" Callbacks: Static method vs. extern "C"
Hi. I need to write a C++ callback function and register it with a C program. I've read that this can be done if the callback function is a static method. I've also read that I should use a global function with the extern "C" prefix. I was leaning toward using the static method approach until I saw a posting that said compatibility between static C++ functions and C is not guaranteed in the respective language standards. This made me start to favor the extern C approach. Any thoughts? Thanks! Ken kk_oop@yahoo.com wrote: > Hi. I need to write a C++ callback funct...

["a", "b", "c", "d"] to "a, b, c, d"?
I want to process each element of an array, but the last element should be handled special. Here is an example: def p_ary(ary) str = "" ary.each do |elem| str << elem << ", " end str.chomp!(", ") str end so p_ary(["a", "f", "x", "test"]) produces "a, f, x, test". The code works, but isn't there an easier and more general way for this behaviour? martinus On Tue, 06 Apr 2004 04:23:22 -0700, Martin wrote: > I want to process each element of an array, but the last ele...

ActiveState vs. "C:\Program Files\" and "C:\Progra~1\"
note: this post started off as a question, but turned into a blog and eventually became an answer. I just hate to have anyone else go through all this nonsense, so I figured I'd post it as an example of "when in doubt, google it out". I successfully installed ActiveState 5.8.8 (after uninstalling 5.6.something) on an XP laptop. I installed it to "C:\Program FIles \Activestate.com\Perl". After the install, I changed the "Program files" part of the ActiveState dirs in my PATH env var from long DOS format to 8.3 format: "C:\Program Files\ActiveState.c...

new URL("c:/dir/file") OR new URL("c:\dir\file") OR new URL("file://c:/dir/file")
new URL("c:/dir/file") OR new URL("c:\dir\file") OR new URL("file://c:/dir/file") What is the correct syntax ? Is it the same as a new File ?FileDialog send: "c:\Program Files\dir\file". I have the feeling that different classes (File, URL, URl) have the different syntax. Please say a), b) or c) and answer me does it is same for all java classes which ask for a file path? FreeStyler =E3=81=AE=E3=83=A1=E3=83=83=E3=82=BB=E3=83=BC=E3=82=B8: > new URL("c:/dir/file") OR new URL("c:\dir\file") OR new > URL("file://c:/dir/fil...

Using "C" modifier to prevent C++ name mangling for DLL function??
have a C++ DLL that needs to be called be external non-C app that uses C interface, but cant figure out how to export C++ DLL functions without name mangling. if I declare _declspec(dllexport) void "C" foo(void) then it compiles ok, but I dont see any foo exported when dumpbin the dll If I leave off the "C" then of course foo is exported but with mangled name tia for any tips/sarcasm perfb@yahoo.com wrote: > have a C++ DLL that needs to be called be external non-C app that uses > C interface, but cant figure out how to export C++ DLL functions > without name...

"exceptional c++", "c++ template programming"
Hello, I am looking for an example of a constructor, I think, which took the type itself for integral or floating types, and a const-ref for a user-defined non primitive type. I saw this in one of these books but I can't find it now: "Exceptional C++" "Exceptional C++ Style" "C++ template programming MPL" if anyone remembers this passage for these books? I found it. It was the MPL book, right before 4.1.2 "Hicham Mouline" <hicham@mouline.org> wrote in message news:49228a87$0$90275$14726298@news.sunsite.dk... > Hello, > I am loo...

What is the difference between "Borland C++" and "Turbo C++"?
...

extern "c" from c++ header files referenced in c files
Hi, I have a mixed c/c++ program, where there are a few .c files where I want to hook to .h files of .cpp files. For example, I have application.c which references an .h file for a ..cpp file (pingpong.h and pingpong.cpp) I've tried to declare c++ style features in my pingpong.h file but it seems that the pingpong.h file keeps on being read as a "C" file. in application.c #include "pingpong.h" in pingpong.h extern "C" { #include <iostream> using namespace std; typedef struct struct_app_pingping { int connectionId; etc.. .... } /*end of extern ...

Corectly convert from %PATH%=c:\\X;"c:\\a;b" TO ['c:\\X', 'c:\\a;b']
Hi, I am trying to treat an environment variable as a python list - and I'm sure there must be a standard and simple way to do so. I know that the interpreter itself must use it (to process $PATH / %PATH%, etc) but I am not able to find a simple function to do so. os.environ['PATH'].split(os.sep) is wrong on Windows for the case when PATH="c:\\A;B";c:\\D; where there is a ';' embedded in the quoted path. Does anyone know of a simple way (addons ok) which would do it in a cross platform way? If not - I will roll my own. My search has shown that generally people ...

c and C++ "same differences"
Hi, This is a serious question brought about by a conversation I have recently with an academic teaching "c" to undergraduates. The lecturer said that they taught C and wanted asked about a reverse engineering tool I have. He then said it was no good if it did not do C++. I ask why if he was teaching C..... The reply frightened me. It was: "We teach C but as scanf and prinf are too difficult for undergraduates we use cin and Cout instead" When I said that C and C++ are not the same language he told me that "C is a subset of C++". He would not believ...

Using the phrase "ISO C++ conformant" (was: Name change to MSDN C++/CLI column)
When I looked up the function "read" in MS Visual Studio 2005, I got the following help: > Run-Time Library Reference > read > > This POSIX function is deprecated beginning in Visual C++ 2005. > Use the ISO C++ conformant _read instead. I suspect Microsoft is not entitled to say that _read is "ISO C++ conformant"; in which way is _read more ISO C++ conformant than read? -- Seungbeom Kim [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] Seungbeom Kim wrote: > When I l...

C"Future library directions" names in C++ <xxx.h> headers
Are the names specified as reserved in "Future library directions" of the C standard also reserved in C++? As an example, in C++, is it proper for the ideal implementation to declare isblank() in the global namespace when <ctype.h> is included? Aaron W. LaFramboise --- [ comp.std.c++ is moderated. To submit articles, try just posting with ] [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ] [ --- Please see the FAQ before posting. --- ] [ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ] ...

c------->c++ and c++------>c
We can write c program in turbo c++ editor.But reverse is not true why? i.e The program will not compile if u write c++ program in turbo c editor vim wrote: > We can write c program in turbo c++ editor.But reverse is not true why? > i.e The program will not compile if u write c++ program in turbo c > editor This isn't about C; this is about some editor. Why the fircone should /we/ know? Ask in a relevant group. [And /why/, in an editor specialised to some specific programming languages, would you expect it to compile a /different/ one? Would you expect turbo perl to compi...

C as a Subset of C++ (or C++ as a superset of C)
Isn't it a lame use of human time and effort to maintain completely separate C and C++ standards? As in the words of Betty White about Facebook: "It seems like an incredible waste of time". Why don't the two standards groups get together and agree on a common specification for the ground which both standards cover? There would still be two separate standards, but they'd both be exactly the same for the common ground. The common ground document could be referred to by both standards instead of being maintained by both groups in individual efforts resulting in...

C/C++ in MATALB or MATLAB in C/C++?
Hi I've been asking on the net how to use MEX properly so that I can use C/C++ in MATLAB, but information is not forthcoming. There seems to be a lot on using MATLAB in C/C++ but not the other way around. I understand Mathworks is trying very hard to sell the Matlab Compiler, but even so, why is it SO hard to get information about MEX? Or is it that I've missed something very fundamental, meaning that I'm struggling over something that is actually quite easy? Any thoughts and info would be great. Thanks BullDog BullDog wrote: > Hi > > I've been asking on the net ...

slime C-c C-c annoyance
Does anyone have a good workaround for this problem other than: just be careful! When I've compiled a large package and slime has given me a *slime-compilation* buffer, I like to work through this buffer issue by issue, fixing the errors, warnings, etc. But If I ever press C-c C-c in the editor window while fixing an error, slime destroys the *(slime-compilation* buffer with the compilation output of C-c C-c. Jim Newton <jimka.issy@gmail.com> writes: > Does anyone have a good workaround for this problem other than: just be careful! > > When I've compiled a ...

C/C++ in matlab and matlab in C/C++ ?
Is it possible to read C/C++ code in matlab and the other way around too? Hi, as long as C or C++ are written in text files you can edit them in the matlab editor. To see the special character of C or C++ coding language, in the editor go to menus: file>preferences>Edito/Debugger>language and on popup "Language" select "C/C++". This does not allow you to run the C/C++ code, just to view it i nicer way... regards, chris saneman <asdfsdf@asd.com> wrote in message <frl846 $f8i$2@news.net.uni-c.dk>... > Is it possible to read C/C...

Could I compile "c" source with a C++ compiler (Forte C++ Update 2)?
Does Forte C++ Update 2 compiles C source code in ANSI C as same as Forte C udpate 2 does? Jacob, Park <bluejacob@empal.com> wrote: > Does Forte C++ Update 2 compiles C source code in ANSI C as same as > Forte C udpate 2 does? If you're asking whether "CC" will compile the same code as "cc", then the answer is no. This is true for ANY standard-compliant c++ compiler (compilers with different "language mode" like gcc don't count as a single compiler for this description). However, usually one can make C code "compilable" by C++ co...

Web resources about - workaround to C "typedef struct name *name;" C vs C++ - comp.lang.c++.moderated

Resources last updated: 2/23/2016 1:04:43 AM