public inbox for gsl-discuss@sourceware.org
 help / color / mirror / Atom feed
* Forward declaration is not possible
@ 2002-12-12  6:08 Peter Haase
  2002-12-13  8:42 ` Brian Gough
  0 siblings, 1 reply; 7+ messages in thread
From: Peter Haase @ 2002-12-12  6:08 UTC (permalink / raw)
  To: gsl-discuss

Hi,
using GSL to implement some random generators, I would like to make a forward 
declaration for 'gsl_rng' and 'gsl_rng_type' (Otherwise, I have to include 
the GSL header files in my header file and clients using my random generators 
must have these GSL header files available ). Unfortunatley a forward 
declaration is not possible since both 'gsl_rng' and 'gsl_rng_type' are not 
names of a struct but are defined by typedef as new types:

(In gsl_rng.h:)

typedef struct 
{
   char* name;
   ....
} gsl_rng;



The reason for this is obvious to avoid the keyword struct when using the name 
gsl_rng in c-code.

My suggestion is to use 'gsl_rng' as a name of the struct and as typename for 
that struct:

(Suggested new code in gsl_rng.h)

typedef struct gsl_rng      // ( ansi c ? if not use 'tag_gsl_rng' instead)
{
   char* name;
   ....
} gsl_rng; 

With that code it is possible to use the gsl as used before and it is possible 
to use a forward declaration and hence to avoid unneccessary #includes of the 
GSL header files. I'm not pretty shure if it's strict ANSI C code when using 
the same token 'gsl_rng' for both the name and the typename (my GCC 3.2 
Compiler does not complain about that). If that is not allowed in ANSI C, the 
token 'tag_gls_rng' could be used for the name of struct (see comment above).
Even that is much more convenient than dealing without a forward declaration.

Peter 

  

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Forward declaration is not possible
  2002-12-12  6:08 Forward declaration is not possible Peter Haase
@ 2002-12-13  8:42 ` Brian Gough
  2002-12-17  9:08   ` Peter Haase
  0 siblings, 1 reply; 7+ messages in thread
From: Brian Gough @ 2002-12-13  8:42 UTC (permalink / raw)
  To: Peter Haase; +Cc: gsl-discuss

Peter Haase writes:
 > Hi, using GSL to implement some random generators, I would like to
 > make a forward declaration for 'gsl_rng' and 'gsl_rng_type'
 > (Otherwise, I have to include the GSL header files in my header
 > file and clients using my random generators must have these GSL
 > header files available ). Unfortunatley a forward declaration is
 > not possible since both 'gsl_rng' and 'gsl_rng_type' are not names
 > of a struct but are defined by typedef as new types

I'm not clear what you want to do on the client side, can you send an
example to motivate it. Thanks.

Brian


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Forward declaration is not possible
  2002-12-13  8:42 ` Brian Gough
@ 2002-12-17  9:08   ` Peter Haase
  2002-12-17 15:22     ` Atakan Gurkan
  2002-12-19 11:40     ` Brian Gough
  0 siblings, 2 replies; 7+ messages in thread
From: Peter Haase @ 2002-12-17  9:08 UTC (permalink / raw)
  To: Brian Gough; +Cc: gsl-discuss

On Friday 13 December 2002 16:50, Brian Gough wrote:
> Peter Haase writes:
>  > Hi, using GSL to implement some random generators, I would like to
>  > make a forward declaration for 'gsl_rng' and 'gsl_rng_type'
>  > (Otherwise, I have to include the GSL header files in my header
>  > file and clients using my random generators must have these GSL
>  > header files available ). Unfortunatley a forward declaration is
>  > not possible since both 'gsl_rng' and 'gsl_rng_type' are not names
>  > of a struct but are defined by typedef as new types
>
> I'm not clear what you want to do on the client side, can you send an
> example to motivate it. Thanks.
>
> Brian

Hi Brian,
I'm writing a library*  that provides some functions that are useful for 
developing simulation programs. For that reason I programmed a class called 
RandomGenerator:

//in myrandom.h:
#include <gsl_rng.h>

class RandomGenerator
{
  public:
    // Class default constructor:
     RandomGenerator();

     //Returns a random variate with  a specified distribution:
     double get();

  private:
     gsl_rng*  ptrToGslRng;
};

A programmer that would like to use my RandomGenerator class in his program 
needs to include my header file (myrandom.h) and to include (implicit via the 
#include in myrandom.h statement)  the gsl header file. From an object 
oriented point of view, the pointer gsl_rng* is just an implemention detail 
that should be hide from the user of that class. So a better programming 
style is:

//in myrandom.h:
// !This isn't needed anymore: #include <gsl_rng.h>!

//Forward declaration:
struct gsl_rng;

class RandomGenerator
{
  public:
    // Class default constructor:
     RandomGenerator();

     //Returns a random variate with  a specified distribution:
     double get();

  private:
     gsl_rng*  ptrToGslRng;
};
 
Unfortunatley this forward declaration is not possible if gsl_rng is declared 
as follows:

typedef struct {
  char* name;
   /*... */
} gsl_rng;

but the forward declaration is possible if gsl_rng is declared as follows:

typedef struct gsl_rng {
  char* name;
  /*... */
} gsl_rng; 

Note, that code that is already existing can be used with that declaration as 
it was before! I'm not shure if it is strict ANSI C to use gsl_rng as a name 
for the struct and as the name of the new type introduced by the typedef 
statement. But at least in C++ it is standard conform (see C++ ARM s. 106): 
'A typedef may be used to redefine a name to refer to the type to which it 
already refers - even in the scope where the type was originally declared. 
For example: 
typedef struct s { /* */ } s;'

*(actually I'm not writing a library, but I think that is a more common case. 
So look to it as an abstract example)

Hope this makes it a bit more clearer, what I would like to do.

Thanks,
Peter

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Forward declaration is not possible
  2002-12-17  9:08   ` Peter Haase
@ 2002-12-17 15:22     ` Atakan Gurkan
  2002-12-19 11:40     ` Brian Gough
  1 sibling, 0 replies; 7+ messages in thread
From: Atakan Gurkan @ 2002-12-17 15:22 UTC (permalink / raw)
  To: gsl-discuss

On Tue, Dec 17, 2002 at 05:13:19PM +0100, Peter Haase wrote:
> Hi Brian,
> I'm writing a library*  that provides some functions that are useful for 
> developing simulation programs. For that reason I programmed a class called 
> RandomGenerator:
> 
> //in myrandom.h:
> #include <gsl_rng.h>
> 
> class RandomGenerator
> {
[deleted]
> };
[deleted]
> 
> //in myrandom.h:
> // !This isn't needed anymore: #include <gsl_rng.h>!
> 
> //Forward declaration:
> struct gsl_rng;
> 
> class RandomGenerator
> {
[deleted]
> };
>  
> Unfortunatley this forward declaration is not possible if gsl_rng is declared 
> as follows:
> 
> typedef struct {
>   char* name;
>    /*... */
> } gsl_rng;
> 
> but the forward declaration is possible if gsl_rng is declared as follows:
> 
> typedef struct gsl_rng {
>   char* name;
>   /*... */
> } gsl_rng; 
> 
> Note, that code that is already existing can be used with that declaration as 
> it was before! I'm not shure if it is strict ANSI C to use gsl_rng as a name 
> for the struct and as the name of the new type introduced by the typedef 
> statement. But at least in C++ it is standard conform (see C++ ARM s. 106): 
> 'A typedef may be used to redefine a name to refer to the type to which it 
> already refers - even in the scope where the type was originally declared. 
> For example: 
> typedef struct s { /* */ } s;'
> 
> *(actually I'm not writing a library, but I think that is a more common case. 
> So look to it as an abstract example)
> 
> Hope this makes it a bit more clearer, what I would like to do.
> 
> Thanks,
> Peter
Hi,
First, my apologies in advance if I am wrong. I did not actually try the idea
that I am about to suggest. Why can you not do something like:
typedef struct gsl_rng gsl_rng_typedefed;

and then use gsl_rng_typedefed for your C++ class? 
I do not mean to object to your proposed change, I have no idea what the 
consequences of that would be, I am just trying to suggest a way for you to get 
things going.

cheers,

ato

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Forward declaration is not possible
  2002-12-17  9:08   ` Peter Haase
  2002-12-17 15:22     ` Atakan Gurkan
@ 2002-12-19 11:40     ` Brian Gough
  2002-12-21 11:47       ` Peter Haase
  1 sibling, 1 reply; 7+ messages in thread
From: Brian Gough @ 2002-12-19 11:40 UTC (permalink / raw)
  To: Peter Haase; +Cc: gsl-discuss

Peter Haase writes:
 > //Forward declaration:
 > struct gsl_rng;

Just to confirm Atakan's reply, it's legal to make a typedef for an
incomplete struct.

typedef struct gsl_rng gsl_rng;

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Forward declaration is not possible
  2002-12-19 11:40     ` Brian Gough
@ 2002-12-21 11:47       ` Peter Haase
  2002-12-23  7:14         ` Brian Gough
  0 siblings, 1 reply; 7+ messages in thread
From: Peter Haase @ 2002-12-21 11:47 UTC (permalink / raw)
  To: Brian Gough; +Cc: gsl-discuss

On Thursday 19 December 2002 20:22, Brian Gough wrote:
> Peter Haase writes: 
>  > //Forward declaration: 
>  > struct gsl_rng; 
> 
> Just to confirm Atakan's reply, it's legal to make a typedef for an
> incomplete struct.
>
> typedef struct gsl_rng gsl_rng;

Hi, trying to compile (with GCC 3.2 under Linux) the file phrand.cpp with code 
in line 41 as follows

//brians suggestion:
typedef struct gsl_rng gsl_rng;  

I get the following error messages:  

/usr/include/gsl/gsl_rng.h:47: conflicting types for `typedef struct gsl_rng 
gsl_rng'
phrand.h:41: previous declaration as `typedef struct gsl_rng gsl_rng'

The same error message occurs with Atakans suggestion:

typedef struct gsl_rng gsl_rng_typedef;

Hence the declaration '... struct gsl_rng ...' within line 41 in 'phrand.cpp' 
conflicts with  'typedef { ... } gsl_rng;' in file 'gsl/gsl_rng.h'

Did I something wrong or is that a non-standard conform behaviour of GCC 3.2??

Peter


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Forward declaration is not possible
  2002-12-21 11:47       ` Peter Haase
@ 2002-12-23  7:14         ` Brian Gough
  0 siblings, 0 replies; 7+ messages in thread
From: Brian Gough @ 2002-12-23  7:14 UTC (permalink / raw)
  To: Peter Haase; +Cc: gsl-discuss

Peter Haase writes:
 > I get the following error messages:  
 > 
 > /usr/include/gsl/gsl_rng.h:47: conflicting types for `typedef struct gsl_rng 
 > gsl_rng'
 > phrand.h:41: previous declaration as `typedef struct gsl_rng gsl_rng'
 > 

You'll need to use an #ifdef to select either the forward declaration
or the gsl header, depending on whether you are compiling the client
or library.

Brian

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2002-12-23 15:13 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-12-12  6:08 Forward declaration is not possible Peter Haase
2002-12-13  8:42 ` Brian Gough
2002-12-17  9:08   ` Peter Haase
2002-12-17 15:22     ` Atakan Gurkan
2002-12-19 11:40     ` Brian Gough
2002-12-21 11:47       ` Peter Haase
2002-12-23  7:14         ` Brian Gough

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).