I can not compile code from <<modern c++ design>>

Matthias Oltmanns Mathias.Oltmanns.Oltmanns@sysde.eads.net
Mon Apr 7 14:42:00 GMT 2003


Am Mon, 2003-04-07 um 15.54 schrieb Di Yang:
> At 13:02 4/7/2003 +0200, Matthias Oltmanns wrote:
> >Am Mon, 2003-04-07 um 11.24 schrieb Di Yang:
> > > Hi,
> > >
> > > public:
> > > enum { exists = sizeof(Test(MakeT())) == sizeof(Small) };
> >
> >Hi,
> >
> >this seems to be not valid C++ code because the calls to the
> >static members 'Test' and 'MakeT' are not const expressions at compile
> >time.
> >For assignments to enum literals there are only integer expressions
> >allowed which are evaluated at compile time.
> >
> >cu
> >Matthias
> 
> Thank you, I changed the code as following and passed GCC 3.2:  -Di
> 
>          template <class T, class U>
>          struct ConversionHelper
>          {
>              typedef char Small;
>              struct Big { char dummy[2]; };
>              static Big   Test(...);
>              static Small Test(U);
>              static T MakeT();
>          };
> 
>      template <class T, class U>
>      struct Conversion
>      {
>          typedef ConversionHelper<T, U> H;
>          enum { exists = sizeof(typename H::Small) == 
> sizeof(H::Test(H::MakeT())) };
> 
>      };
> ......
> 
> 
> C:\ttt>g++ -o template template.cpp
> template.cpp: In instantiation of `Conversion<double, int>':
> template.cpp:40:   instantiated from here
> template.cpp:40: warning: passing `double' for argument 1 of `static char
>     ConversionHelper<T, U>::Test(U) [with T = double, U = int]'
> 
> C:\ttt>template
> 1 0
> C:\ttt>

Hi,

i'am confused. Your changes shows that I'm wrong. But i' cant see why.
You could also change your first version making all members public:

class Conversion
{
public:
  typedef char Small;
  class Big { char dummy[2]; };
  
  static Small Test(U);
  static Big Test(...);
  static T MakeT();
  
  enum { exists = sizeof(Test(MakeT())) == sizeof(Small) };
};

which also works.

So, if the initialization of the enum is legal it should be legal in
your previous version having only the enum public.

cu
Matthias




More information about the Gcc-help mailing list