[comp.lang.c++] inline member functions returning a enumerated type

chris@island.uu.net (Chris King) (07/24/90)

Hi.
  I could use some help figuring out a problem I am having with
defining a inline member function that returns a enum. I don't
understand the compile time error I get when I try to do this. I am
using Sun's version of cfront 2.0. Here is a simple test program
showing the problem and cfronts output. 


struct test {
    enum id { A, B, C };
    
    enum id v;

    test() { v = A; }
    inline enum id GetV() { return v; }

};

main()
{
    test t;

    enum id V = t.GetV();
}


"TestClass.cc", line 8: error: bad base type: inline enum id


Thanks in advance for any help that you can give me.


Chris King
Island Graphics Corp.
{sun,ucbcad,uunet}!island!chris
						

mao@hpclmao.HP.COM (Mike Ogush) (07/27/90)

> / hpclmao:comp.lang.c++ / chris@island.uu.net (Chris King) / 10:31 am  Jul 23, 1990 /
> Hi.
>   I could use some help figuring out a problem I am having with
> defining a inline member function that returns a enum. I don't
> understand the compile time error I get when I try to do this. I am
> using Sun's version of cfront 2.0. Here is a simple test program
> showing the problem and cfronts output. 
> 

    .
  Code omitted
    .

> 
> "TestClass.cc", line 8: error: bad base type: inline enum id
> 
> 
> Thanks in advance for any help that you can give me.
> 
> 
> Chris King
> Island Graphics Corp.
> {sun,ucbcad,uunet}!island!chris
> 						
> ----------

I this out on HPs C++ compiler and got the same result.  I suspect that the
parser objects to "inline enum id" as a valid type.  In any case, I
modified your code to rename the "enum id" type using typedef and the code
compiled fine.

Fixed version:

struct test {
    typedef enum id { A, B, C } ID;

    ID v;

    test() { v = A; }
    inline ID GetV() { return v; }

};

main()
{
    test t;

    ID V = t.GetV();
}

--

    Mike Ogush

    California Language Laboratory  
    Systems Technology Division
    Hewlett-Packard Company

    ...!hplabs!hpclmao!mao  or  mao%hpclmao@hplabs.hp.com

glenn@bitstream.com (Glenn P. Parker) (07/27/90)

In article <1851@island.uu.net> chris@island.uu.net (Chris King) writes:
>   I could use some help figuring out a problem I am having with
> defining a inline member function that returns a enum. I don't
> ...
> struct test {
>     enum id { A, B, C };
>     
>     enum id v;
> 
>     test() { v = A; }
>     inline enum id GetV() { return v; }
      ^^^^^^
> };
> ...
> "TestClass.cc", line 8: error: bad base type: inline enum id

Apollo C++ 2.0 gives the same error, if that's any comfort.

Removing the indicated "inline", which is superfluous (but certainly not
illegal), makes the error go away.  The function will still be inlined,
because it is declared within the struct definition.  Alternatively,
removing the "enum" following the indicated "inline" will _also_ make the
error go away.  Again, the enum is superfluous (but not illegal, I think),
since "id" is sufficient to identify the type of the function.

Looks like a compiler bug to me, but nothing too serious.
--
--------------------------------------------------------------------------
Glenn P. Parker        Bitstream, Inc.
uunet!huxley!glenn     215 First Street
glenn@bitstream.com    Cambridge, MA 02142-1270

lynch@cerc.utexas.edu (Tom Lynch) (07/27/90)

In article <GLENN.90Jul26170701@huxley.bitstream.com> <glenn@bitstream.com> (Glenn Parker) writes:
>In article <1851@island.uu.net> chris@island.uu.net (Chris King) writes:
>>   I could use some help figuring out a problem I am having with
>> defining a inline member function that returns a enum. I don't
>> ...
>> struct test {
>>     enum id { A, B, C };
>>     
>>     enum id v;
>> 
>>     test() { v = A; }
>>     inline enum id GetV() { return v; }
>      ^^^^^^
>> };
>> ...
>> "TestClass.cc", line 8: error: bad base type: inline enum id
>
>Apollo C++ 2.0 gives the same error, if that's any comfort.
>
>Removing the indicated "inline", which is superfluous (but certainly not
>illegal), makes the error go away.  The function will still be inlined,
>because it is declared within the struct definition.  Alternatively,
>removing the "enum" following the indicated "inline" will _also_ make the
>error go away.  Again, the enum is superfluous (but not illegal, I think),
>since "id" is sufficient to identify the type of the function.
>
>Looks like a compiler bug to me, but nothing too serious.


In this case 'id' is already an enum type, so adding the 'enum' in 
front of it is erroneous.  Also, one should not place the extra 'struct' in
front of structure types.

p309 of The C++ Programming Language by Bjarne Strouistup, r14,
Differences from C:
  "The name of a class or enumeration is a type name"


of the same book, Note to C Programmers

"The better on knows C, the harder it seems to avoid writing C++ in
 C style ..." 

tom
lynch@cerc.utexas.edu

lijewski@batcomputer.tn.cornell.edu (Mike Lijewski) (07/28/90)

In article <1990Jul27.083837.2078@cerc.utexas.edu> lynch@cerc.utexas.edu (Tom Lynch) writes:
 
>In this case 'id' is already an enum type, so adding the 'enum' in 
>front of it is erroneous.  Also, one should not place the extra 'struct' in
>front of structure types.

Adding an "extra" enum (or any typename for that matter) is not erroneous.
In fact, there are cases in C++ programming (see appended example) where
you are be forced to write 'enum id' instead of just 'id'.

>p309 of The C++ Programming Language by Bjarne Strouistup, r14,
>Differences from C:
>  "The name of a class or enumeration is a type name"

Very true.  But unlike C, typenames and non-type names are in the same
name space C++.  So sometimes you must use the typename explicitely to
differentiate between a typename and non-type name.
  
>of the same book, Note to C Programmers
>"The better on knows C, the harder it seems to avoid writing C++ in
> C style ..." 
>
>tom
>lynch@cerc.utexas.edu

If you're going to post quotes from books, please use E&S.

/****** EXAMPLE ******/

enum S { A, B, C };

int
main()
{
    int S;
    S s;  // this is an error; must be 'enum S s;'

    return 0;
}


-- 
Mike Lijewski  (H)607/277-0394 (W)607/254-8686
Cornell National Supercomputer Facility
ARPA: mjlx@eagle.cnsf.cornell.edu  BITNET: mjlx@cornellf.bitnet
SMAIL:  1122 Ellis Hollow Rd. Ithaca, NY  14850

lynch@cerc.utexas.edu (Tom Lynch) (07/28/90)

In article <10589@batcomputer.tn.cornell.edu> lijewski@tcgould.tn.cornell.edu (Mike Lijewski) writes:
>In article <1990Jul27.083837.2078@cerc.utexas.edu> lynch@cerc.utexas.edu (Tom Lynch) writes:
> 
>>In this case 'id' is already an enum type, so adding the 'enum' in 
>>front of it is erroneous.  Also, one should not place the extra 'struct' in
>>front of structure types.
>
>Adding an "extra" enum (or any typename for that matter) is not erroneous.
>In fact, there are cases in C++ programming (see appended example) where
>you are be forced to write 'enum id' instead of just 'id'.
>
>>p309 of The C++ Programming Language by Bjarne Strouistup, r14,
>>Differences from C:
>>  "The name of a class or enumeration is a type name"
>
>Very true.  But unlike C, typenames and non-type names are in the same
>name space C++.  So sometimes you must use the typename explicitely to
>differentiate between a typename and non-type name.
>  
...
>
>/****** EXAMPLE ******/
>
>enum S { A, B, C };
>
>int
>main()
>{
>    int S;
>    S s;  // this is an error; must be 'enum S s;'
>
>    return 0;
>}
>
>
 mike, I tried your example:

enum S {A,B,C};
int
main()
{
  int S
  enum S s;
  
  return 0;
}

and got:

test.C: In function int main ():
test.C:6: parse error before `enum'

got the same error the original poster did.  Hmmm, Could you provide
a reference to kind of typing you are demonstrating - I looked in
Lippman and of course Stroupstrup and couldn't find it. Please use
E&S.  

tom
lynch@cerc.utexas.edu        Nothing is absolute.

hg@tsisj1.UUCP (Heinrich Gantenbein) (07/28/90)

In article <58170029@hpclmao.HP.COM> mao@hpclmao.HP.COM (Mike Ogush) writes:
>> / hpclmao:comp.lang.c++ / chris@island.uu.net (Chris King) / 10:31 am  Jul 23, 1990 /
>> [asks for help on inline enum id foobar () {return v;}
>> "TestClass.cc", line 8: error: bad base type: inline enum id
>parser objects to "inline enum id" as a valid type.  In any case, I
>modified your code to rename the "enum id" type using typedef and the code
>compiled fine.
>
>Fixed version:
>
>struct test {
>    typedef enum id { A, B, C } ID;
>
>    ID v;
>
>    test() { v = A; }
>    inline ID GetV() { return v; }
>
>};
[rest deleted]

An enum tag under C++ is already a typedef so the above is redundancy (both
the original and the CORRECTED version. 
A look at c_varieties.h in at least the SUN version of C++ 2.0 indicates
that specifying enum my_type my_func (... as illegal. They provide a macro
to deal with the differences:
-- start of included segment from comment section of that file --
 * enum type specifier:
 *	In K&R C, it is OK to use "enum xyz" as return type but in C++,
 * 	one should use "xyz".  ENUM_TYPE macro is used to handle this.
 *
 *		ENUM_TYPE(enum, xyz) (*func) (...);
 -- end --
 
 I hope this helps. And please do not use typedefs to dupplicate one of the
 great features of C++ :-).
 
--
--
Heinrich Gantenbein, Software Consultant:   I'd rather be sailing.
hg@tsisj1.uucp
..!apple!ditka!tsisj1!hg  ...!mips!daver!ditka!hg

lynch@cerc.utexas.edu (Tom Lynch) (07/28/90)

In article <1990Jul27.234044.8701@cerc.utexas.edu> lynch@cerc.utexas.edu (Tom Lynch) writes:
>In article <10589@batcomputer.tn.cornell.edu> lijewski@tcgould.tn.cornell.edu (Mike Lijewski) writes:
>>In article <1990Jul27.083837.2078@cerc.utexas.edu> lynch@cerc.utexas.edu (Tom Lynch) writes:
>> 
>>>In this case 'id' is already an enum type, so adding the 'enum' in 
>>>front of it is erroneous.  Also, one should not place the extra 'struct' in
>>>front of structure types.
>>
>>Adding an "extra" enum (or any typename for that matter) is not erroneous.
>>In fact, there are cases in C++ programming (see appended example) where
>>you are be forced to write 'enum id' instead of just 'id'.
>>
>>>p309 of The C++ Programming Language by Bjarne Strouistup, r14,
>>>Differences from C:
>>>  "The name of a class or enumeration is a type name"
>>
>>Very true.  But unlike C, typenames and non-type names are in the same
>>name space C++.  So sometimes you must use the typename explicitely to
>>differentiate between a typename and non-type name.
>>  
>...
>>
>>/****** EXAMPLE ******/
>>
>>enum S { A, B, C };
>>
>>int
>>main()
>>{
>>    int S;
>>    S s;  // this is an error; must be 'enum S s;'
>>
>>    return 0;
>>}
>>
>>
> mike, I tried your example:
>
>enum S {A,B,C};
>int
>main()
>{
>  int S
>  enum S s;
>  
>  return 0;
>}
>
>and got:
>
  
My God! it compiles fine with a ';'
  my mistake.  I am very embarrassed - please excuse the posting.
  
-tom