IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

 C++ Discussion :

Visual : 0 warnings vs. gcc : 100+ erreurs


Sujet :

C++

  1. #1
    Membre confirmé
    Avatar de Captain'Flam
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Février 2011
    Messages
    273
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Février 2011
    Messages : 273
    Points : 455
    Points
    455
    Billets dans le blog
    1
    Par défaut Visual : 0 warnings vs. gcc : 100+ erreurs
    Bonjour à tous !

    Je sollicite votre aide car j'ai eu les yeux plus gros que le ventre...
    J'ai créé une classe qui permet de calculer sur des entiers "embrouillés".
    L'idée est de manipuler des entiers qui ne sont pas "en clair" en mémoire, sans avoir à les désembrouiller.

    Le principe est simple : mes entiers sont codés dans une base B mais stockés dans une base T.
    Je m'explique (c'est un peu matheux, mais pas trop) :

    Si X est un entier < B^N, alors son écriture en base B ressemble à :

    Somme ( i dans 0..N-1 ) de x[i] * B^i

    Le tableau x contient N entiers entre 0 et B-1.
    Par exemple, dans notre bonne vieille base 10 :

    X = 273 = 3 + 7*10 + 2*100 = 3*10^0 + 7*10^1 + 2*10^2
    on a x[3] = { 3,7,2 }

    Donc, partant de là, je me suis dis : pourquoi ne pas convertir mon nombre en base B puis le stocker en l’interprétant comme un nombre en base T (on doit avoir T >= B si on ne veut pas perdre d'information).

    Exemple : 273(en base 10) = 2043 (en base 5)
    en effet 3 + 4*5 + 0*5^2 + 2*5^3 = 3 + 20 + 2*125 = 273

    Mais ce nombre 2043 peut aussi s’interpréter comme un nombre en base 7.
    Dans ce cas, on a : 3 + 4*7 + 0*7^2 + 2*7^3 = 3 + 28 + 686 = 717

    Et voilà comment 273 se retrouve "embrouillé" en 717.
    L'astuce, c'est que je peux continuer à faire des calculs arithmétiques dessus.

    Et tout ça, ça marche impec' avec un belle classe template dont les arguments sont :
    le type d'entiers utiliser pour stocker mon nombre embrouillé et les bases B et T.

    Si T est une puissance de 2, alors je peux optimiser puisque chaque chiffre de mon nombre est stocké dans un groupe de bits distincts.
    En utilisant un petit peu de méta programmation, j'arrive à une classe qui détecte toute seule si T est une puissance de 2 et utilise un mode de calcul optimisé.

    Maintenant que je vous ai bien ennuyé avec toute cette histoire, voici le problème :

    Mon code compile (et fonctionne) parfaitement sous Visual 2010 (niveau de warning à 4), mais provoque une cascade d'erreurs avec gcc !
    Et là, je coince...
    J'aimerais avoir un code full compatible gcc mais je ne comprends rien à la bordée d'injures qu'il m'envoie.

    J'aurais aimé (comme je le fais souvent sur ce forum) n'envoyer qu'un sous ensemble de mon code pour vous faciliter la lecture, mais là, rien à faire, c'est trop imbriqué...

    Donc voici mon code dans son intégralité () suivi des messages de gcc :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
     
    // classe de base pour "meta programmer" les fonctions de bases de la classe integer dont elle hérite
    template<typename num,int coded_base,int stored_base,int ispow2> struct integer_base {} ;
     
    // spécialisation pour 'stored_base' qui n'est pas une puissance de 2
    template<typename num,int coded_base,int stored_base> class integer_base<num,coded_base,stored_base,0>
        {
        public:
            typedef num type ;
     
        protected:
            type value ;
     
            static const int B = coded_base ;  // base source : 0 <= digit < B
            static const int T = stored_base ; // base de stockage
            static       int N ;               // nb de digit dans un 'type'
     
            static const type max_type = (((type)1 << (sizeof( type )*4))-1)*(((type)1 << (sizeof( type )*4))+1) ; // astuce (n-1)*(n+1) == n*n - 1
            static const int  div_size = sizeof( type )*8+1 ;
     
            static type div [div_size] ;
            static int zero ; // ce membre n'est là que pour être initialisé par un appel à 'pre_compute'
            static int pre_compute () // je n'arrive pas à forcer l'appel à 'pre_compute' autrement...
                {
                type b = 1 ;
                type t = 1 ;
                type biggest = 0 ;
                for ( max = 0 , N = 0 ;; N++ , b *= B , t *= T )
                    {
                    if (t > (max_type - biggest)/(B-1) )
                        break ;
                    if ((N > 0) && (t/T != div[N-1]))
                        break ;
                    biggest += (B-1)*t ;
                    max     += (B-1)*b ;
                    div[N]   = t ;
                    }
                return 0 ;
                }
     
            int d ( int i ) const // lecture du digit i
                {
                return i < N ? (int)(value/div[i] % T) : 0 ;
                }
     
            bool d ( int i , int x )  // écriture du digit i -> renvoie false en cas de dépassement
                {
                if (i >= N) return (x == 0) ;
                int y = (value/div[i]) % T ;
                if (x == y) return true ;
                if (y > x)
                    value -= (type)(y-x)*div[i] ;
                else
                    value += (type)(x-y)*div[i] ;
                return true ;
                }
        public:
            static type max ;
        } ;
     
    template<typename num,int coded_base,int stored_base> typename integer_base<num,coded_base,stored_base,0>::type integer_base<num,coded_base,stored_base,0>::div [] ;
    template<typename num,int coded_base,int stored_base> typename integer_base<num,coded_base,stored_base,0>::type integer_base<num,coded_base,stored_base,0>::max ;
    template<typename num,int coded_base,int stored_base> int                                                       integer_base<num,coded_base,stored_base,0>::N ;
    template<typename num,int coded_base,int stored_base> int                                                       integer_base<num,coded_base,stored_base,0>::zero = integer_base<num,coded_base,stored_base,0>::pre_compute() ;
     
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     
    // spécialisation pour 'stored_base' qui est une puissance de 2
    template<typename num,int coded_base,int stored_base> class integer_base<num,coded_base,stored_base,1>
        {
        public:
            typedef num type ;
     
        protected:
            type value ;
     
            template<int X,int P> struct pow       { static const num result = X*pow<X,P-1>::result ;} ;
            template<int X>       struct pow<X,0>  { static const num result = 1 ;} ;
     
            template<int X>       struct log2      { static const num result = 1+log2<X/2>::result ;} ;
            template<>            struct log2<0>   { static const num result = 0 ;} ;
     
            static const int B = coded_base ;                // base : 0 <= digit < B
            static const int T = log2<stored_base>::result ; // nb bit par digit
            static const int N = (int)(sizeof( type )*8/T) ; // nb de digit dans un 'type'
            static const int zero = 0 ;
            static const type max_type = (((type)1 << (sizeof( type )*4))-1)*(((type)1 << (sizeof( type )*4))+1) ;
     
            int d ( int i ) const
                {
                return i < N ? (int)((value >> i*T)%(1<<T)) : 0 ;
                }
     
            bool d ( int i , int x )
                {
                if (i >= N) return (x == 0) ;
                value = (type)((value & ~((((type)1<<T)-1) << (i*T))) | ((type)x << (i*T))) ;
                return true ;
                }
     
        public:
            static const type max = pow<B,N>::result-1 ;
        } ;
     
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     
    // classe qui permet de calculer sur des entiers qui restent offusqués en mémoire
    template<typename num,int coded_base,int stored_base>
    class integer : public integer_base<num,coded_base,stored_base,(~(stored_base-1)&stored_base)==stored_base>
        {
        public:
            typedef num type ;
     
            integer ( unsigned x = 0 )
                {
                *this = (unsigned)(x+zero) ; // utilisation du membre 'zero' pour forcer l'appel à 'pre_compute'...
                }
     
            integer ( const integer & x )
                {
                value = x.value ;
                }
     
            integer operator= ( unsigned x )
                {
                value = 0 ;
                for ( int i = 0 ; x > 0 ; x /= B , i++ )
                    if (!d( i,x%B ))
                        {
                        value = max_type ;
                        break ;
                        }
                return *this ;
                }
     
            integer operator= ( const integer & x )
                {
                value = x.value ;
                return *this ;
                }
     
            bool error () const
                {
                return value == max_type ;
                }
     
            unsigned get () const
                {
                if (error()) return (unsigned)-1 ;
                type res = 0 ;
                type p   = 1 ;
                for ( int i = 0 ; i < N ; i++ , p *= B )
                    res += (type)(d( i )*p) ;
                return (unsigned)res ;
                }
     
            integer operator+ ( const integer & x ) const
                {
                if (error() || x.error()) return integer(false) ;
                integer res ;
                int c = 0 ;
                for ( int i = 0 ; i <= N ; i++ )
                    {
                    int b = d(i) + x.d(i) + c ;
                    c = b/B ;
                    if (!res.d( i,b%B ))
                        return integer(false) ;
                    }
                return res ;
                }
     
            integer operator* ( const integer & x ) const
                {
                if (error() || x.error()) return integer(false) ;
                integer res ;
                for ( int j = 0 ; j < N ; j++ )
                    {
                    int    dj = d(j) ;
                    int     c = 0 ;
                    integer t ;
                    for ( int i = 0 ; i <= N ; i++ )
                        {
                        int b = dj * x.d(i) + c ;
                        c = b/B ;
                        if (!t.d( i+j,b%B ))
                            return integer(false) ;
                        }
                     res = res+t ;
                    if (res.error())
                        return res ;
                    }
                return res ;
                }
     
            template <typename output>
            friend output & operator<< ( output & o , const integer & i )
                {
                return i.error() ? o << "overflow" : o << i.get() ;
                }
     
            template <typename output>
            friend output & operator<< ( output & o , const integer * i )
                {
                bool z = !true ;
                for ( int n = N-1 ; n >= 0 ; n-- )
                    {
                    z = z && (i->d( n ) == 0) ;
                    if (!z)
                        {
                        o << i->d( n ) ;
                        if (n > 0) o << '.' ;
                        }
                    }
                if (z) o << 0 ;
                return o ;
                }
     
        protected:
            integer ( bool )  { value = max_type ;}  // construit un 'integer' en état d'erreur
        } ;
     
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    /////  tests  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     
    #include <stdlib.h>
    #include <iostream>
    using namespace std ;
     
    template <typename num,int coded,int stored> bool sub_test0 ( unsigned a , unsigned b , bool verbose = false )
        {
        unsigned long long aa = a ;
        unsigned long long bb = b ;
     
        integer<num,coded,stored> x = a ;
        integer<num,coded,stored> y = x+b ;
     
        if (verbose)
            {
            cout << "sub_test0<" ;
            switch ( sizeof( num ))
                {
                case 8 : cout << "unsigned long long," ; break ;
                case 4 : cout << "unsigned," ; break ;
                case 2 : cout << "unsigned short," ; break ;
                case 1 : cout << "unsigned byte,"  ; break ;
                }
            cout << coded << ',' << stored << ">( " << a << ',' << b << " ) [" << x.max << "]  " ;
            }
     
        if ( y.error() && (aa+bb <= x.max))
            if (!verbose)
                return sub_test0<num,coded,stored>( a,b,true ) ;
            else
                {
                cout << "error but " << a << '+' << b << " = " << aa+bb << " <= " << x.max << endl ;
                return false ;
                }
     
        if (!y.error() && (aa+bb != y.get()))
            if (!verbose)
                return sub_test0<num,coded,stored>( a,b,true ) ;
            else
                {
                cout << "no error but " << a << '+' << b << " = " << aa+bb << " != " << y << endl ;
                return false ;
                }
     
        y = x*b ;
        if ( y.error() && (aa*bb > 0) && (aa*bb <= x.max))
            if (!verbose)
                return sub_test0<num,coded,stored>( a,b,true ) ;
            else
                {
                cout << "error but " << a << '*' << b << " = " << aa*bb << " <= " << x.max << endl ;
                return false ;
                }
     
        if (!y.error() && (a*b != y.get()))
            if (!verbose)
                return sub_test0<num,coded,stored>( a,b,true ) ;
            else
                {
                cout << "no error but " << a << '*' << b << " = " << a*b << " != " << y << endl ;
                return false ;
                }
     
        return true ;
        }
     
    template <typename num,int a,int b> bool sub_test1 ( unsigned x , unsigned y )
        {
        bool ok = true ;
        ok = ok && sub_test0<num,2+a  ,2+b  >( x,y ) ;
        ok = ok && sub_test0<num,2+a  ,2+b+1>( x,y ) ;
        ok = ok && sub_test0<num,2+a+1,2+b+1>( x,y ) ;
        return ok ;
        }
     
    template <typename num,int a,int b> bool sub_test2 ( unsigned x , unsigned y )
        {
        bool ok = true ;
        ok = ok && sub_test1<num,a  ,b  >( x,y ) ;
        ok = ok && sub_test1<num,a  ,b+2>( x,y ) ;
        ok = ok && sub_test1<num,a+2,b+2>( x,y ) ;
        return ok ;
        }
     
    template <typename num,int a,int b> bool sub_test3 ( unsigned x , unsigned y )
        {
        bool ok = true ;
        ok = ok && sub_test2<num,a  ,b  >( x,y ) ;
        ok = ok && sub_test2<num,a  ,b+4>( x,y ) ;
        ok = ok && sub_test2<num,a+4,b+4>( x,y ) ;
        return ok ;
        }
     
    template <typename num,int a,int b> bool sub_test4 ( unsigned x , unsigned y )
        {
        bool ok = true ;
        ok = ok && sub_test3<num,a  ,b  >( x,y ) ;
        ok = ok && sub_test3<num,a  ,b+4>( x,y ) ;
        ok = ok && sub_test3<num,a+4,b+4>( x,y ) ;
        return ok ;
        }
     
    unsigned random ( int nbbit )
        {
        unsigned res = ((rand()&0xff) << 24) | ((rand()&0xff) << 16) | ((rand()&0xff) << 8) | (rand()&0xff) ;
        while ( nbbit++ < 32 ) res >>= 1 ;
        return res ;
        }
     
    bool test ()
        {
        for ( int nb = 0 ; nb < 10000 ; nb++ )
            {
            unsigned x = random( 1+rand()%31 ) ;
            unsigned y = random( 1+rand()%31 ) ;
            bool ok = true ;
            ok = ok && sub_test4<unsigned long long,0,0>( x,y ) ;
            ok = ok && sub_test4<unsigned long     ,0,0>( x,y ) ;
            ok = ok && sub_test4<unsigned short    ,0,0>( x,y ) ;
            ok = ok && sub_test4<unsigned char     ,0,0>( x,y ) ;
            if (!ok) return false ;
            }
        return true ;
        }
     
    //////////////////////////////////////////////////////////////////////////
     
    int main ()
        {
        if (test())
            cout << "ok" << endl ;
        else
            cout << "ERROR !!!" << endl ;
        return 0 ;
        }
    et voici ce qu'en pense gcc :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
     
    >gcc main.cpp -Wall -lstdc++
    main.cpp:82:18: error: explicit specialization in non-namespace scope 'class integer_base<num, coded_base, stored_base, 1>'
    main.cpp:82:38: error: template parameters not used in partial specialization:
    main.cpp:82:38: error:         'num'
    main.cpp:82:38: error:         'coded_base'
    main.cpp:82:38: error:         'stored_base'
    main.cpp: In constructor 'integer<num, coded_base, stored_base>::integer(unsigned int)':
    main.cpp:117:34: error: 'zero' was not declared in this scope
    main.cpp: In copy constructor 'integer<num, coded_base, stored_base>::integer(const integer<num, coded_base, stored_base>&)':
    main.cpp:122:13: error: 'value' was not declared in this scope
    main.cpp: In member function 'integer<num, coded_base, stored_base> integer<num, coded_base, stored_base>::operator=(unsigned int)':
    main.cpp:127:13: error: 'value' was not declared in this scope
    main.cpp:128:44: error: 'B' was not declared in this scope
    main.cpp:129:31: error: there are no arguments to 'd' that depend on a template parameter, so a declaration of 'd' must be available
    main.cpp:129:31: note: (if you use '-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)
    main.cpp:131:29: error: 'max_type' was not declared in this scope
    main.cpp: In member function 'integer<num, coded_base, stored_base> integer<num, coded_base, stored_base>::operator=(const integer<num, coded_base, stored_base>&)':
    main.cpp:139:13: error: 'value' was not declared in this scope
    main.cpp: In member function 'bool integer<num, coded_base, stored_base>::error() const':
    main.cpp:145:20: error: 'value' was not declared in this scope
    main.cpp:145:29: error: 'max_type' was not declared in this scope
    main.cpp: In member function 'unsigned int integer<num, coded_base, stored_base>::get() const':
    main.cpp:153:35: error: 'N' was not declared in this scope
    main.cpp:153:50: error: 'B' was not declared in this scope
    main.cpp:154:36: error: there are no arguments to 'd' that depend on a template parameter, so a declaration of 'd' must be available
    main.cpp: In member function 'integer<num, coded_base, stored_base> integer<num, coded_base, stored_base>::operator+(const integer<num, coded_base, stored_base>&) const':
    main.cpp:163:36: error: 'N' was not declared in this scope
    main.cpp:165:28: error: there are no arguments to 'd' that depend on a template parameter, so a declaration of 'd' must be available
    main.cpp:166:23: error: 'B' was not declared in this scope
    main.cpp: In member function 'integer<num, coded_base, stored_base> integer<num, coded_base, stored_base>::operator*(const integer<num, coded_base, stored_base>&) const':
    main.cpp:177:35: error: 'N' was not declared in this scope
    main.cpp:179:32: error: there are no arguments to 'd' that depend on a template parameter, so a declaration of 'd' must be available
    main.cpp:185:27: error: 'B' was not declared in this scope
    main.cpp: In function 'output& operator<<(output&, const integer<num, coded_base, stored_base>*)':
    main.cpp:206:27: error: 'N' was not declared in this scope
    main.cpp: In constructor 'integer<num, coded_base, stored_base>::integer(bool)':
    main.cpp:220:29: error: 'value' was not declared in this scope
    main.cpp:220:37: error: 'max_type' was not declared in this scope
    main.cpp: In function 'bool sub_test0(unsigned int, unsigned int, bool)':
    main.cpp:252:8: warning: suggest explicit braces to avoid ambiguous 'else'
    main.cpp:261:8: warning: suggest explicit braces to avoid ambiguous 'else'
    main.cpp:271:8: warning: suggest explicit braces to avoid ambiguous 'else'
    main.cpp:280:8: warning: suggest explicit braces to avoid ambiguous 'else'
    main.cpp: At global scope:
    main.cpp:81:89: error: template instantiation depth exceeds maximum of 1024 (use -ftemplate-depth= to increase the maximum) instantiating 'integer_base<long long unsigned int, 2, 2, 1>::log2<0>::result'
    main.cpp:81:89:   instantiated from 'const long long unsigned int integer_base<long long unsigned int, 2, 2, 1>::log2<0>::result'
    main.cpp:81:89:   instantiated from 'const long long unsigned int integer_base<long long unsigned int, 2, 2, 1>::log2<0>::result'
    main.cpp:81:89:   instantiated from 'const long long unsigned int integer_base<long long unsigned int, 2, 2, 1>::log2<0>::result'
    main.cpp:81:89:   instantiated from 'const long long unsigned int integer_base<long long unsigned int, 2, 2, 1>::log2<0>::result'
    main.cpp:81:89:   instantiated from 'const long long unsigned int integer_base<long long unsigned int, 2, 2, 1>::log2<0>::result'
    main.cpp:81:89:   [ skipping 1014 instantiation contexts ]
    main.cpp:249:9:   instantiated from 'bool sub_test0(unsigned int, unsigned int, bool) [with num = long long unsigned int, int coded = 2, int stored = 2]'
    main.cpp:295:5:   instantiated from 'bool sub_test1(unsigned int, unsigned int) [with num = long long unsigned int, int a = 0, int b = 0]'
    main.cpp:304:5:   instantiated from 'bool sub_test2(unsigned int, unsigned int) [with num = long long unsigned int, int a = 0, int b = 0]'
    main.cpp:313:5:   instantiated from 'bool sub_test3(unsigned int, unsigned int) [with num = long long unsigned int, int a = 0, int b = 0]'
    main.cpp:322:5:   instantiated from 'bool sub_test4(unsigned int, unsigned int) [with num = long long unsigned int, int a = 0, int b = 0]'
    main.cpp:342:59:   instantiated from here
     
    main.cpp:81:89: error: template instantiation depth exceeds maximum of 1024 (use -ftemplate-depth= to increase the maximum) instantiating 'integer_base<long long unsigned int, 2, 2, 1>::log2<0>::result'
    main.cpp:81:89:   instantiated from 'const long long unsigned int integer_base<long long unsigned int, 2, 2, 1>::log2<0>::result'
    main.cpp:81:89:   instantiated from 'const long long unsigned int integer_base<long long unsigned int, 2, 2, 1>::log2<0>::result'
    main.cpp:81:89:   instantiated from 'const long long unsigned int integer_base<long long unsigned int, 2, 2, 1>::log2<0>::result'
    main.cpp:81:89:   instantiated from 'const long long unsigned int integer_base<long long unsigned int, 2, 2, 1>::log2<0>::result'
    main.cpp:81:89:   instantiated from 'const long long unsigned int integer_base<long long unsigned int, 2, 2, 1>::log2<0>::result'
    main.cpp:81:89:   [ skipping 1014 instantiation contexts ]
    main.cpp:249:9:   instantiated from 'bool sub_test0(unsigned int, unsigned int, bool) [with num = long long unsigned int, int coded = 2, int stored = 2]'
    main.cpp:295:5:   instantiated from 'bool sub_test1(unsigned int, unsigned int) [with num = long long unsigned int, int a = 0, int b = 0]'
    main.cpp:304:5:   instantiated from 'bool sub_test2(unsigned int, unsigned int) [with num = long long unsigned int, int a = 0, int b = 0]'
    main.cpp:313:5:   instantiated from 'bool sub_test3(unsigned int, unsigned int) [with num = long long unsigned int, int a = 0, int b = 0]'
    main.cpp:322:5:   instantiated from 'bool sub_test4(unsigned int, unsigned int) [with num = long long unsigned int, int a = 0, int b = 0]'
    main.cpp:342:59:   instantiated from here
     
    main.cpp:81:89: error: 'integer_base<long long unsigned int, 2, 2, 1>::log2<0>::result' cannot be initialized by a non-constant expression when being declared
    main.cpp: In instantiation of 'const long long unsigned int integer_base<long long unsigned int, 2, 2, 1>::log2<1>::result':
    main.cpp:81:89:   instantiated from 'const long long unsigned int integer_base<long long unsigned int, 2, 2, 1>::log2<2>::result'
    main.cpp:85:49:   instantiated from 'const int integer_base<long long unsigned int, 2, 2, 1>::T'
    main.cpp:86:54:   instantiated from 'const int integer_base<long long unsigned int, 2, 2, 1>::N'
    main.cpp:103:50:   instantiated from 'const integer_base<long long unsigned int, 2, 2, 1>::type integer_base<long long unsigned int, 2, 2, 1>::max'
    main.cpp:249:9:   instantiated from 'bool sub_test0(unsigned int, unsigned int, bool) [with num = long long unsigned int, int coded = 2, int stored = 2]'
    main.cpp:295:5:   instantiated from 'bool sub_test1(unsigned int, unsigned int) [with num = long long unsigned int, int a = 0, int b = 0]'
    main.cpp:304:5:   instantiated from 'bool sub_test2(unsigned int, unsigned int) [with num = long long unsigned int, int a = 0, int b = 0]'
    main.cpp:313:5:   instantiated from 'bool sub_test3(unsigned int, unsigned int) [with num = long long unsigned int, int a = 0, int b = 0]'
    main.cpp:322:5:   instantiated from 'bool sub_test4(unsigned int, unsigned int) [with num = long long unsigned int, int a = 0, int b = 0]'
    main.cpp:342:59:   instantiated from here
    main.cpp:81:89: error: 'integer_base<long long unsigned int, 2, 2, 1>::log2<1>::result' cannot be initialized by a non-constant expression when being declared
    main.cpp: In instantiation of 'const long long unsigned int integer_base<long long unsigned int, 2, 2, 1>::log2<2>::result':
    main.cpp:85:49:   instantiated from 'const int integer_base<long long unsigned int, 2, 2, 1>::T'
    main.cpp:86:54:   instantiated from 'const int integer_base<long long unsigned int, 2, 2, 1>::N'
    main.cpp:103:50:   instantiated from 'const integer_base<long long unsigned int, 2, 2, 1>::type integer_base<long long unsigned int, 2, 2, 1>::max'
    main.cpp:249:9:   instantiated from 'bool sub_test0(unsigned int, unsigned int, bool) [with num = long long unsigned int, int coded = 2, int stored = 2]'
    main.cpp:295:5:   instantiated from 'bool sub_test1(unsigned int, unsigned int) [with num = long long unsigned int, int a = 0, int b = 0]'
    main.cpp:304:5:   instantiated from 'bool sub_test2(unsigned int, unsigned int) [with num = long long unsigned int, int a = 0, int b = 0]'
    main.cpp:313:5:   instantiated from 'bool sub_test3(unsigned int, unsigned int) [with num = long long unsigned int, int a = 0, int b = 0]'
    main.cpp:322:5:   instantiated from 'bool sub_test4(unsigned int, unsigned int) [with num = long long unsigned int, int a = 0, int b = 0]'
    main.cpp:342:59:   instantiated from here
    main.cpp:81:89: error: 'integer_base<long long unsigned int, 2, 2, 1>::log2<2>::result' cannot be initialized by a non-constant expression when being declared
    Si je sors la struct log2 de la classe, j'ai un peu moins d'erreur, mais je ne comprends pas pourquoi...
    D'autant que pow juste à côté ne semble pas poser de problème...

    Donc, ceux qui sont arrivés jusque là l'auront compris, les gcc' guys qui voudraient bien éclairer ma lanterne sont les biens venus...

    Merci de m'avoir lu et encore plus de me répondre
    Captain'Flam
    anciennement Sopsag, aka Hadrien
    Win seven x64 & Win 10 / Visual 2017 / Python 2.7 / Eclipse

  2. #2
    Inactif  


    Homme Profil pro
    Doctorant sécurité informatique — Diplômé master Droit/Économie/Gestion
    Inscrit en
    Décembre 2011
    Messages
    9 012
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 31
    Localisation : France, Loire (Rhône Alpes)

    Informations professionnelles :
    Activité : Doctorant sécurité informatique — Diplômé master Droit/Économie/Gestion
    Secteur : Enseignement

    Informations forums :
    Inscription : Décembre 2011
    Messages : 9 012
    Points : 23 145
    Points
    23 145
    Par défaut
    Bonjour,

    Sans vouloir te décevoir, il te manque encore des warning vu que tu n'as pas activé le flags -Wextra.

    Pour la spécialisation de template, utilise la surcharge de fonction à la place, ce sera plus simple.

    Certaines erreurs sont très explicites et je pense que tu pourrais les corriger comme
    "zero" was not declared in this scope
    .

    Sinon, pour rendre ton code plus lisible, essaye de séparer la déclaration de ta classe et l'implémentation des méthodes de ta classe.

    Essaye déjà de corriger toi-même tous ces "was not declared in this scope".


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    template<typename num,int coded_base,int stored_base,int ispow2> struct integer_base {} ;
    Ta classe integer_base est vide par défaut.

    Je ne comprend même pas que VS accepte de compiler...

    D'ailleurs es-tu sûr qu'il faille utiliser 0 et 1 pour le dernier argument de ton template et pas plutôt "true" et "false" ?

    Ton code est aussi relativement crade, on ne met pas des break dans un for !

    Ta classe template n'a pas besoin de :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    int coded_base,int stored_base
    Comme argument de ton template, met les plutôt comme paramètre de ton constructeur.

    Pourquoi utiliser une structure log/pow au lieu d'une fonction?

  3. #3
    Membre confirmé
    Avatar de Captain'Flam
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Février 2011
    Messages
    273
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Février 2011
    Messages : 273
    Points : 455
    Points
    455
    Billets dans le blog
    1
    Par défaut
    Merci de ta réponse Neckara, mais je n'avance guère...

    Je ne comprends toujours pas pourquoi zero n'est pas visible dans le scope.
    Je ne comprends pas non pourquoi est-ce un problème que integer_base soit vide.

    De toutes façons, integer ne peut hériter que d'une des 2 versions versions spécialisées.
    Donc, logiquement, ce que je pourrais mettre dans la integer_base "générique" ne serait pas visible dans integer.
    Il n'y a pas de notion d'héritage entre un template et sa version spécialisée. si ?

    Si int coded_base,int stored_base sont des paramètres de template, et si log et pow sont méta programmées, c'est pour avoir un maximum de valeurs constantes qui permettent d'optimiser les calculs.

    Merci !
    Captain'Flam
    anciennement Sopsag, aka Hadrien
    Win seven x64 & Win 10 / Visual 2017 / Python 2.7 / Eclipse

  4. #4
    Inactif  


    Homme Profil pro
    Doctorant sécurité informatique — Diplômé master Droit/Économie/Gestion
    Inscrit en
    Décembre 2011
    Messages
    9 012
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 31
    Localisation : France, Loire (Rhône Alpes)

    Informations professionnelles :
    Activité : Doctorant sécurité informatique — Diplômé master Droit/Économie/Gestion
    Secteur : Enseignement

    Informations forums :
    Inscription : Décembre 2011
    Messages : 9 012
    Points : 23 145
    Points
    23 145
    Par défaut
    Citation Envoyé par Captain'Flam Voir le message
    Je ne comprends toujours pas pourquoi zero n'est pas visible dans le scope.
    Par ce que integer_base est vide.

    De toutes façons, integer ne peut hériter que d'une des 2 versions versions spécialisées.
    Donc, logiquement, ce que je pourrais mettre dans la integer_base "générique" ne serait pas visible dans integer.
    Il n'y a pas de notion d'héritage entre un template et sa version spécialisée. si ?
    Non, je pense que tu as mal compris la notion de template, ce n'est pas de l'héritage.
    Tu redéfini le comportement de ton template en fonction dans le as où un argument template aurait telle valeur. Or, de base, ta structure n'a aucun comportement.

    Si int coded_base,int stored_base sont des paramètres de template, et si log et pow sont méta programmées, c'est pour avoir un maximum de valeurs constantes qui permettent d'optimiser les calculs.
    La règle d'or est d'éviter le plus possible les optimisations prématurée.
    Il faut d'abord avoir un code lisible, maintenable et fonctionnel, ensuite on utilise des logiciels de profilage pour voir les goulots d'étranglements et ensuite on optimise.

    Pour int coded_base,int stored_base, je ne pense pas que ce soit une optimisation, au contraire, à vérifier avec des tests de profilage.

    Pour log et pow, tu peux faire des fonctions inlines constexpr. Ce sera bien plus performant et lisible.

  5. #5
    Membre confirmé
    Avatar de Captain'Flam
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Février 2011
    Messages
    273
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Février 2011
    Messages : 273
    Points : 455
    Points
    455
    Billets dans le blog
    1
    Par défaut
    Voici un petit exemple minimaliste qui reproduit le cas :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
     
    template<int N,int P> struct foo_base
        {
        static const int n = N*P ;
        };
     
    template<int N> struct foo : foo_base<N,4>
        {
        int x ;
        foo ()
            {
            x = n+1 ;
            }
        };
     
    struct bar_base
        {
        static const int n = 7 ;
        };
     
    struct bar : bar_base
        {
        int x ;
        bar ()
            {
            x = n+1 ;
            }
        };
     
     
    int main ()
        {
        foo<3> f ;
        bar    b ;
        }
    et voici ce qu'en dit gcc :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    >gcc test.cpp -Wall -lstdc++
    test.cpp: In constructor 'foo<N>::foo()':
    test.cpp:11:13: error: 'n' was not declared in this scope
    On voit bien que c'est l'utilisation des templates qui change tout...

    Pour info, j'utilise gcc 4.5.4 20111030.
    Captain'Flam
    anciennement Sopsag, aka Hadrien
    Win seven x64 & Win 10 / Visual 2017 / Python 2.7 / Eclipse

  6. #6
    Inactif  


    Homme Profil pro
    Doctorant sécurité informatique — Diplômé master Droit/Économie/Gestion
    Inscrit en
    Décembre 2011
    Messages
    9 012
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 31
    Localisation : France, Loire (Rhône Alpes)

    Informations professionnelles :
    Activité : Doctorant sécurité informatique — Diplômé master Droit/Économie/Gestion
    Secteur : Enseignement

    Informations forums :
    Inscription : Décembre 2011
    Messages : 9 012
    Points : 23 145
    Points
    23 145
    Par défaut
    Etrange...

    Essaye d'avoir une version plus récente (4.8.2) de g++.

    Sinon avec this->n tu as la même erreur ?

    EDIT : il faut utiliser this->n.
    Je ne sais pas pourquoi, si c'est à cause de la norme ou à cause de g++ qui ne regarde pas correctement les template.
    Cela me surprend un peu

  7. #7
    En attente de confirmation mail

    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Août 2004
    Messages
    1 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 33
    Localisation : France, Doubs (Franche Comté)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Août 2004
    Messages : 1 391
    Points : 3 311
    Points
    3 311
    Par défaut
    http://www.comeaucomputing.com/techt.../#whythisarrow

    Ta base dépend d'un paramètre template, le compilateur ne sais pas ce qu'il y a dedans avant instanciation. Dit lui que l'identifiant sera bien dans cette portée, voir le lien pour les solution (this->, using, :.

  8. #8
    Membre confirmé
    Avatar de Captain'Flam
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Février 2011
    Messages
    273
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Février 2011
    Messages : 273
    Points : 455
    Points
    455
    Billets dans le blog
    1
    Par défaut
    Merci Flob90, c'est exactement l'explication de mon étrange phénomène...
    En effet, un petit coup de this-> et tout rentre dans l'ordre.

    Si je comprends bien, et si je me souviens bien de mes cours de compilation, le problème viendrait de ce que le compilateur lève cette erreur avant d'avoir résolu tous les types template.

    En théorie, ce ne devrait pas être une erreur puisqu'à la fin, une fois que les paramètres template sont remontés jusqu'à la racine de l'arbre d'héritage, il peut bien vérifier que les symboles existent.
    D'ailleurs, comment s'y prend cl (le compilo de Visual) pour ne pas générer
    d'erreur ?

    Est-ce qu'il est plus malin () ou juste plus permissif () ?
    Captain'Flam
    anciennement Sopsag, aka Hadrien
    Win seven x64 & Win 10 / Visual 2017 / Python 2.7 / Eclipse

  9. #9
    Inactif  


    Homme Profil pro
    Doctorant sécurité informatique — Diplômé master Droit/Économie/Gestion
    Inscrit en
    Décembre 2011
    Messages
    9 012
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 31
    Localisation : France, Loire (Rhône Alpes)

    Informations professionnelles :
    Activité : Doctorant sécurité informatique — Diplômé master Droit/Économie/Gestion
    Secteur : Enseignement

    Informations forums :
    Inscription : Décembre 2011
    Messages : 9 012
    Points : 23 145
    Points
    23 145
    Par défaut
    Je comprend maintenant pourquoi g++ voit cela comme une erreur.

    D'ailleurs le fait que VS soit plus permissif à ce niveau là est une hérésie.
    Neckara vient de gagner un nouvel argument pour troller VS \o/

    En effet, pour le moment ça "marche" pour ton code. Mais imagine qu'un jour, tu rajoutes un bout de code qui donne à ta classe B un paramètre template jusqu'ici imprévu et paff... ça marche plus.
    Donc au final, VS ne fait que cacher une erreur que le distributeur de la bibliothèque ne verra pas mais auxquels ses utilisateurs seront confrontés (par exemple).

  10. #10
    Membre confirmé
    Avatar de Captain'Flam
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Février 2011
    Messages
    273
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Février 2011
    Messages : 273
    Points : 455
    Points
    455
    Billets dans le blog
    1
    Par défaut


    En ajoutant une armée de using, j'ai enfin atteint le graaaal de tout programmeur qui se respecte : 0 warning ! (et même avec -Wextra pour faire plaisir à Neckara )
    Captain'Flam
    anciennement Sopsag, aka Hadrien
    Win seven x64 & Win 10 / Visual 2017 / Python 2.7 / Eclipse

  11. #11
    Inactif  


    Homme Profil pro
    Doctorant sécurité informatique — Diplômé master Droit/Économie/Gestion
    Inscrit en
    Décembre 2011
    Messages
    9 012
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 31
    Localisation : France, Loire (Rhône Alpes)

    Informations professionnelles :
    Activité : Doctorant sécurité informatique — Diplômé master Droit/Économie/Gestion
    Secteur : Enseignement

    Informations forums :
    Inscription : Décembre 2011
    Messages : 9 012
    Points : 23 145
    Points
    23 145
    Par défaut
    Bien, maintenant, tu ouvres Code:Block, tu active toutes les options et tu t'amuses à corriger la flopée des nouveaux warnings

    Plus sérieusement, "Si tu n'as pas de Warnings, c'est que tu n'as pas activé suffisamment d'options".

  12. #12
    Expert éminent sénior
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 369
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 369
    Points : 41 518
    Points
    41 518
    Par défaut
    D'un autre côté, il y a des warnings abusifs. Je me souviens encore de gcc 3.4.2 qui t'affichais un warning "missing initializer" pour struct toto s = {0} et ne supportait pas l'option -Wno-missing-initializers...

    Ou bien, le "unreferenced inline function" quand la fonction inline en question est dans un header inclut par plusieurs fichiers...
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

  13. #13
    Membre confirmé
    Avatar de Captain'Flam
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Février 2011
    Messages
    273
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Février 2011
    Messages : 273
    Points : 455
    Points
    455
    Billets dans le blog
    1
    Par défaut
    Rien de plus pénible que d'alourdir son code juste pour faire plaisir au compilateur...
    Captain'Flam
    anciennement Sopsag, aka Hadrien
    Win seven x64 & Win 10 / Visual 2017 / Python 2.7 / Eclipse

  14. #14
    Inactif  


    Homme Profil pro
    Doctorant sécurité informatique — Diplômé master Droit/Économie/Gestion
    Inscrit en
    Décembre 2011
    Messages
    9 012
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 31
    Localisation : France, Loire (Rhône Alpes)

    Informations professionnelles :
    Activité : Doctorant sécurité informatique — Diplômé master Droit/Économie/Gestion
    Secteur : Enseignement

    Informations forums :
    Inscription : Décembre 2011
    Messages : 9 012
    Points : 23 145
    Points
    23 145
    Par défaut
    Les warnings ne sont pas juste là pour t'enquiquiner

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Eviter les mélanges de C et C++ avec un warning dans gcc
    Par erroneus dans le forum Autres éditeurs
    Réponses: 1
    Dernier message: 25/05/2010, 01h15
  2. [Visual Studio 2005] mt.exe renvoie "erreur 31"
    Par Trap D dans le forum Visual C++
    Réponses: 2
    Dernier message: 23/09/2007, 11h14
  3. Import WSDL dans Visual Studio : warning
    Par kaboume dans le forum Services Web
    Réponses: 2
    Dernier message: 30/08/2007, 16h47
  4. Réponses: 5
    Dernier message: 08/01/2007, 23h17
  5. Rediriger les warnings et messages d'erreur
    Par biggir dans le forum Langage
    Réponses: 3
    Dernier message: 31/03/2005, 15h20

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo