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?
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
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
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 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
-----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 ==============================================================================
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
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++?
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
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
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.
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 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
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
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
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
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
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
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
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
programming@lists.opensuse.org