[opensuse-programming] gcc/g++ difference in handling a GNU extension
I have a question about a difference between gcc and g++ in the following: typedef struct { const char *CMD_SERVICE, *CMD_TRANSPORT; } dPavueCnfg; dPavueCnfg dConfig = { .CMD_SERVICE = "DPcmd", .CMD_TRANSPORT = "tcp" }; This works with gcc (on Linux and MinGW). But it does not work in g++. So, is this a GNU C-only extension? Given that it is a GNU extension, it could have been done for both gcc and g++. I am not even sure what this feature is called to be able to search for it! Perhaps this support is defined at compiler build time? -- Roger Oberholtzer OPQ Systems / Ramböll RST Ramböll Sverige AB Krukmakargatan 21 P.O. Box 17009 SE-104 62 Stockholm, Sweden Office: Int +46 10-615 60 20 Mobile: Int +46 70-815 1696 -- To unsubscribe, e-mail: opensuse-programming+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-programming+help@opensuse.org
On Wednesday 02 June 2010 12:46:51 Roger Oberholtzer wrote:
This works with gcc (on Linux and MinGW). But it does not work in g++. So, is this a GNU C-only extension? Given that it is a GNU extension, it could have been done for both gcc and g++. I am not even sure what this feature is called to be able to search for it! Perhaps this support is defined at compiler build time? http://gcc.gnu.org/onlinedocs/gcc-4.4.1/gcc/Designated-Inits.html#Designated... Inits
Presumably it's the "This extension is not implemented in GNU C++." bit that's relevant to what you're seeing Anders -- To unsubscribe, e-mail: opensuse-programming+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-programming+help@opensuse.org
Roger Oberholtzer wrote:
I have a question about a difference between gcc and g++ in the following:
typedef struct {
const char *CMD_SERVICE, *CMD_TRANSPORT;
} dPavueCnfg;
dPavueCnfg dConfig = {
.CMD_SERVICE = "DPcmd", .CMD_TRANSPORT = "tcp" };
This works with gcc (on Linux and MinGW). But it does not work in g++. So, is this a GNU C-only extension?
No, designated initializers belong to C99, but they're not part of C++. /Per Jessen, Zürich -- To unsubscribe, e-mail: opensuse-programming+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-programming+help@opensuse.org
On Wed, 2010-06-02 at 13:19 +0200, Per Jessen wrote:
Roger Oberholtzer wrote:
I have a question about a difference between gcc and g++ in the following:
typedef struct {
const char *CMD_SERVICE, *CMD_TRANSPORT;
} dPavueCnfg;
dPavueCnfg dConfig = {
.CMD_SERVICE = "DPcmd", .CMD_TRANSPORT = "tcp" };
This works with gcc (on Linux and MinGW). But it does not work in g++. So, is this a GNU C-only extension?
No, designated initializers belong to C99, but they're not part of C++.
D*mn. I am using a 3-rd party library that is c++, and so i am compiling with g++. I usually do not use it. Oh well. I will have to adapt. At least I know what this is called: designated initializers. They are a good thing IMHO. I thought they were GNU extensions. So if they are C99, other compilers might support them? As we have really extended our use of them, I was thinking we were effectively deciding that we were going to stick with GNU compilers. -- Roger Oberholtzer OPQ Systems / Ramböll RST Ramböll Sverige AB Krukmakargatan 21 P.O. Box 17009 SE-104 62 Stockholm, Sweden Office: Int +46 10-615 60 20 Mobile: Int +46 70-815 1696 -- To unsubscribe, e-mail: opensuse-programming+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-programming+help@opensuse.org
Roger Oberholtzer wrote:
On Wed, 2010-06-02 at 13:19 +0200, Per Jessen wrote:
Roger Oberholtzer wrote:
I have a question about a difference between gcc and g++ in the following:
typedef struct {
const char *CMD_SERVICE, *CMD_TRANSPORT;
} dPavueCnfg;
dPavueCnfg dConfig = {
.CMD_SERVICE = "DPcmd", .CMD_TRANSPORT = "tcp" };
This works with gcc (on Linux and MinGW). But it does not work in g++. So, is this a GNU C-only extension?
No, designated initializers belong to C99, but they're not part of C++.
D*mn. I am using a 3-rd party library that is c++, and so i am compiling with g++. I usually do not use it. Oh well. I will have to adapt.
I know exactly what you mean - I've recently started using xapian, also C++. I wish there were a g++ switch for enabling their use.
At least I know what this is called: designated initializers. They are a good thing IMHO.
Most definitely - makes the initialization safer and far easier to read.
I thought they were GNU extensions. So if they are C99, other compilers might support them? As we have really extended our use of them, I was thinking we were effectively deciding that we were going to stick with GNU compilers.
I'm pretty certain other compilers such as e.g the Intel and the Pathscale compilers will support C99 too. /Per Jessen, Zürich -- To unsubscribe, e-mail: opensuse-programming+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-programming+help@opensuse.org
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Per Jessen wrote:
Roger Oberholtzer wrote:
On Wed, 2010-06-02 at 13:19 +0200, Per Jessen wrote:
Roger Oberholtzer wrote:
I have a question about a difference between gcc and g++ in the following:
typedef struct {
const char *CMD_SERVICE, *CMD_TRANSPORT;
} dPavueCnfg;
dPavueCnfg dConfig = {
.CMD_SERVICE = "DPcmd", .CMD_TRANSPORT = "tcp" };
This works with gcc (on Linux and MinGW). But it does not work in g++. So, is this a GNU C-only extension? No, designated initializers belong to C99, but they're not part of C++. D*mn. I am using a 3-rd party library that is c++, and so i am compiling with g++. I usually do not use it. Oh well. I will have to adapt.
I know exactly what you mean - I've recently started using xapian, also C++. I wish there were a g++ switch for enabling their use.
At least I know what this is called: designated initializers. They are a good thing IMHO.
Most definitely - makes the initialization safer and far easier to read.
I thought they were GNU extensions. So if they are C99, other compilers might support them? As we have really extended our use of them, I was thinking we were effectively deciding that we were going to stick with GNU compilers.
I'm pretty certain other compilers such as e.g the Intel and the Pathscale compilers will support C99 too.
/Per Jessen, Zürich
I think I have come across a couple of references suggesting that an object constructor could supply equivalent functionality. The above construct is thought by some to be functionally problematic within the C++ OO model. (IIRC there is a school of thought that data structures are best implemented as data objects in OOP anyway). - -- ============================================================================== I have always wished that my computer would be as easy to use as my telephone. My wish has come true. I no longer know how to use my telephone. Bjarne Stroustrup ============================================================================== -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.9 (GNU/Linux) Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org/ iEYEARECAAYFAkwHkBUACgkQasN0sSnLmgLTnQCfVEl+Ky9yuaq10CuHCxqlcQbc K+0An0JE8HzHfaN6HT+ZuP0uM022wRcP =1bHK -----END PGP SIGNATURE----- -- To unsubscribe, e-mail: opensuse-programming+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-programming+help@opensuse.org
G T Smith wrote:
I think I have come across a couple of references suggesting that an object constructor could supply equivalent functionality.
There's no doubt it could, but I think the key thing would be to have the same syntax for C and C++ (in this respect) and to allow C code adhering to C99 to be compiled with g++. /Per Jessen, Zürich -- To unsubscribe, e-mail: opensuse-programming+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-programming+help@opensuse.org
On Thu, 2010-06-03 at 14:01 +0200, Per Jessen wrote:
G T Smith wrote:
I think I have come across a couple of references suggesting that an object constructor could supply equivalent functionality.
There's no doubt it could, but I think the key thing would be to have the same syntax for C and C++ (in this respect) and to allow C code adhering to C99 to be compiled with g++.
Exactly! What would be the equivalent functionality in g++? I am not a c++ programmer, so a concrete example is needed! The C version of one use is this: typedef struct { const char *CMD_SERVICE, *CMD_TRANSPORT; } dPavueCnfg; dPavueCnfg dConfig = { .CMD_SERVICE = "DPcmd", .CMD_TRANSPORT = "tcp" }; What would I do in C++? -- Roger Oberholtzer OPQ Systems / Ramböll RST Ramböll Sverige AB Krukmakargatan 21 P.O. Box 17009 SE-104 62 Stockholm, Sweden Office: Int +46 10-615 60 20 Mobile: Int +46 70-815 1696 -- To unsubscribe, e-mail: opensuse-programming+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-programming+help@opensuse.org
On Thu, 03 Jun 2010, 15:55:20 +0200, Roger Oberholtzer wrote:
On Thu, 2010-06-03 at 14:01 +0200, Per Jessen wrote:
G T Smith wrote:
I think I have come across a couple of references suggesting that an object constructor could supply equivalent functionality.
There's no doubt it could, but I think the key thing would be to have the same syntax for C and C++ (in this respect) and to allow C code adhering to C99 to be compiled with g++.
Exactly!
What would be the equivalent functionality in g++? I am not a c++ programmer, so a concrete example is needed!
The C version of one use is this:
typedef struct {
const char *CMD_SERVICE, *CMD_TRANSPORT;
} dPavueCnfg;
dPavueCnfg dConfig = {
.CMD_SERVICE = "DPcmd", .CMD_TRANSPORT = "tcp" };
What would I do in C++?
The same you would have done in C before C99: dPavueCnfg dConfig = { "DPcmd", "tcp" }; I.e. ensure the initializers match the same position their respective struct elements have! Using the names of the struct tags makes it much more easy to avoid errors, but this is apparently something the C++ standardization people haven't though about (yet) ;-) HTH, cheers. l8er manfred -- To unsubscribe, e-mail: opensuse-programming+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-programming+help@opensuse.org
Manfred Hollstein wrote:
Using the names of the struct tags makes it much more easy to avoid errors, but this is apparently something the C++ standardization people haven't though about (yet) ;-)
AFAIK, the new C++ standard doesn't have this feature either, but perhaps the thinking is that you should be using classes rather than structs in C++. /Per Jessen, Zürich -- To unsubscribe, e-mail: opensuse-programming+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-programming+help@opensuse.org
On Thu, Jun 03, 2010 at 05:02:31PM +0200, Per Jessen wrote:
Manfred Hollstein wrote:
Using the names of the struct tags makes it much more easy to avoid errors, but this is apparently something the C++ standardization people haven't though about (yet) ;-)
AFAIK, the new C++ standard doesn't have this feature either, but perhaps the thinking is that you should be using classes rather than structs in C++.
Then they should instead introduce support for named parameters for methods or at least constructors. ;-) Petr "Pasky" Baudis -- To unsubscribe, e-mail: opensuse-programming+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-programming+help@opensuse.org
Roger Oberholtzer wrote:
What would be the equivalent functionality in g++? I am not a c++ programmer, so a concrete example is needed!
The C version of one use is this:
typedef struct {
const char *CMD_SERVICE, *CMD_TRANSPORT;
} dPavueCnfg;
dPavueCnfg dConfig = {
.CMD_SERVICE = "DPcmd", .CMD_TRANSPORT = "tcp" };
What would I do in C++?
It's been about ten years since I did C++ regularly, but let's try: your struct becomes a class class xxxxxx { public: const char *CMD_SERVICE, *CMD_TRANSPORT; // constructor xxxxxxx( char *s, char *t):CMD_SERVICE(s),CMD_TRANSPORT(t) {}; } When you instantiate the class: xxxxxx dConfig ( "DPcmd", "tcp" ); /Per Jessen, Zürich -- To unsubscribe, e-mail: opensuse-programming+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-programming+help@opensuse.org
On Thu, 2010-06-03 at 16:23 +0200, Per Jessen wrote:
Roger Oberholtzer wrote:
What would be the equivalent functionality in g++? I am not a c++ programmer, so a concrete example is needed!
The C version of one use is this:
typedef struct {
const char *CMD_SERVICE, *CMD_TRANSPORT;
} dPavueCnfg;
dPavueCnfg dConfig = {
.CMD_SERVICE = "DPcmd", .CMD_TRANSPORT = "tcp" };
What would I do in C++?
It's been about ten years since I did C++ regularly, but let's try:
your struct becomes a class
class xxxxxx {
public: const char *CMD_SERVICE, *CMD_TRANSPORT;
// constructor xxxxxxx( char *s, char *t):CMD_SERVICE(s),CMD_TRANSPORT(t) {}; }
When you instantiate the class:
xxxxxx dConfig ( "DPcmd", "tcp" );
/Per Jessen, Zürich
The thing is, I do not want to use it from c++. It is used in C code. The whole application is C. I am just forced to use the g++ compiler because some include files for a library I must use (an interface to some GigaEvision cameras) use c++ definitions. Even though the stuff I am using from them are all C. Such is life. -- Roger Oberholtzer OPQ Systems / Ramböll RST Ramböll Sverige AB Krukmakargatan 21 P.O. Box 17009 SE-104 62 Stockholm, Sweden Office: Int +46 10-615 60 20 Mobile: Int +46 70-815 1696 -- To unsubscribe, e-mail: opensuse-programming+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-programming+help@opensuse.org
Roger Oberholtzer wrote:
The thing is, I do not want to use it from c++. It is used in C code. The whole application is C. I am just forced to use the g++ compiler because some include files for a library I must use (an interface to some GigaEvision cameras) use c++ definitions. Even though the stuff I am using from them are all C. Such is life.
I think you're left with perhaps creating a glue layer when you create C functions for the C++ interfaces you need. That's what I did. /Per Jessen, Zürich -- To unsubscribe, e-mail: opensuse-programming+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-programming+help@opensuse.org
On Thu, Jun 03, 2010 at 04:23:59PM +0200, Per Jessen wrote:
your struct becomes a class
You can keep it a struct...
class xxxxxx {
public: const char *CMD_SERVICE, *CMD_TRANSPORT;
// constructor xxxxxxx( char *s, char *t):CMD_SERVICE(s),CMD_TRANSPORT(t) {}; }
When you instantiate the class:
xxxxxx dConfig ( "DPcmd", "tcp" );
...but this is strictly _worse_ than using C89-style initializer - you get the same information value and an extra need to write a constructor and keep it up to date. The poin of C99-style initializers is that you (i) explicitly annotate values by the fields they initialize and (ii) can leave some fields uninitialized. I think C++ simply has no equivalent functionality that would allow for (i) and (ii)? -- Petr "Pasky" Baudis The true meaning of life is to plant a tree under whose shade you will never sit. -- To unsubscribe, e-mail: opensuse-programming+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-programming+help@opensuse.org
Petr Baudis wrote:
On Thu, Jun 03, 2010 at 04:23:59PM +0200, Per Jessen wrote:
your struct becomes a class
You can keep it a struct...
But can you have constructors for a struct?
When you instantiate the class:
xxxxxx dConfig ( "DPcmd", "tcp" );
...but this is strictly _worse_ than using C89-style initializer - you get the same information value and an extra need to write a constructor and keep it up to date. The poin of C99-style initializers is that you (i) explicitly annotate values by the fields they initialize and (ii) can leave some fields uninitialized.
Completely agree, I wasn't suggesting anyone should do this. /Per Jessen, Zürich -- To unsubscribe, e-mail: opensuse-programming+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-programming+help@opensuse.org
On Thu, Jun 03, 2010 at 04:58:49PM +0200, Per Jessen wrote:
Petr Baudis wrote:
On Thu, Jun 03, 2010 at 04:23:59PM +0200, Per Jessen wrote:
your struct becomes a class
You can keep it a struct...
But can you have constructors for a struct?
In C++, struct is just like a class, except that the default visibility is public. Petr "Pasky" Baudis -- To unsubscribe, e-mail: opensuse-programming+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-programming+help@opensuse.org
Petr Baudis wrote:
On Thu, Jun 03, 2010 at 04:58:49PM +0200, Per Jessen wrote:
Petr Baudis wrote:
On Thu, Jun 03, 2010 at 04:23:59PM +0200, Per Jessen wrote:
your struct becomes a class
You can keep it a struct...
But can you have constructors for a struct?
In C++, struct is just like a class, except that the default visibility is public.
I guess I've got some reading to do - so if I declare a struct called 'jessen', I could declare a function called 'jessen' which would act as the constructor?? /Per Jessen, Zürich -- To unsubscribe, e-mail: opensuse-programming+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-programming+help@opensuse.org
On Thursday June 3 2010, Per Jessen wrote:
You can keep it a struct... But can you have constructors for a struct? In C++, struct is just like a class, except that the default visibility is public.
I guess I've got some reading to do - so if I declare a struct called 'jessen', I could declare a function called 'jessen' which would act as the constructor??
A haphazard approach to learning to program in C++ cannot be recommended. Find some tutorial material.
/Per Jessen, Zürich
Randall Schulz -- To unsubscribe, e-mail: opensuse-programming+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-programming+help@opensuse.org
Randall R Schulz wrote:
On Thursday June 3 2010, Per Jessen wrote:
You can keep it a struct... But can you have constructors for a struct? In C++, struct is just like a class, except that the default visibility is public.
I guess I've got some reading to do - so if I declare a struct called 'jessen', I could declare a function called 'jessen' which would act as the constructor??
A haphazard approach to learning to program in C++ cannot be recommended. Find some tutorial material.
I think you may have neglected to read one of my earlier postings. /Per Jessen, Zürich -- To unsubscribe, e-mail: opensuse-programming+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-programming+help@opensuse.org
participants (7)
-
Anders Johansson
-
G T Smith
-
Manfred Hollstein
-
Per Jessen
-
Petr Baudis
-
Randall R Schulz
-
Roger Oberholtzer