CRTP e controllo sizeof del tipo "derivato"
Von: GM (gm@no.spam.it) [Profil]
Datum: 16.06.2008 11:12
Message-ID: <48562e56$0$40158$4fafbaef@reader1.news.tin.it>
Newsgroup: it.comp.lang.c++
Datum: 16.06.2008 11:12
Message-ID: <48562e56$0$40158$4fafbaef@reader1.news.tin.it>
Newsgroup: it.comp.lang.c++
Vorrei controllare staticamente la sizeof di un tipo che viene passato
come parametro template nel "pattern" CRTP (tipo "derivato").
Ovviamente questa cosa al compilatore non piace perchè quando si trova a
dover valutare sizeof(D) D non è ancora definito.
Per tentare di aggirare l'ostacolo ho messo il check nel distruttore
della classe base, così almeno il check viene eseguito quando viene
creata un'istanza, ma avrei preferito che venisse eseguito alla
dichiarazione del tipo derivato a prescindere dalla creazione di istanze.
Secondo voi è possibile?
Esempio
#include <boost/static_assert.hpp>
template <typename D>
struct B
{
~B()
{
BOOST_STATIC_ASSERT(sizeof(D)==4); //OK
}
BOOST_STATIC_ASSERT(sizeof(D)==4); //<- ERRORE!
//: error C2027: use of undefined type 'aaa'
};
//questa dovrebbe compilare
//sizeof(aaa) == 4)
struct aaa : B<aaa>
{
int b;
};
//questa vorrei che non compilasse
//a prescindere dal fatto che poi
//di bbb vengano create istanze o meno
//sizeof(bbb) != 4)
struct bbb : B<bbb>
{
int a;
int b;
};
GM
[ Auf dieses Posting antworten ]
