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

Di Yang diy@qualcomm.com
Mon Apr 7 13:54:00 GMT 2003


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>

>--
>Matthias Oltmanns
>
>Tel: 04421-1543-274
>mail: Mathias.Oltmanns.Oltmanns@sysde.eads.net



More information about the Gcc-help mailing list