2

「this」ポインタを参照する静的アサートを書くことは可能ですか? 私は c++11 を利用できず、BOOST_STATIC_ASSERT が機能しません。

struct blah
{
   void func() {BOOST_STATIC_ASSERT(sizeof(*this));}
};

プロデュース:

error C2355: 'this' : can only be referenced inside non-static member functions
error C2027: use of undefined type 'boost::STATIC_ASSERTION_FAILURE'

MSVC 2008 で。

動機:

#define CLASS_USES_SMALL_POOL() \
   void __small_pool_check()     {BOOST_STATIC_ASSERT(sizeof(*this) < SMALL_MALLOC_SIZE;} \
   void* operator new(size_t)    {return SmallMalloc();}                                  \
   void operator delete(void* p) {SmallFree(p);}
4

2 に答える 2

2

問題はBOOST_STATIC_ASSERTマクロthisあり、キーワードが異なる意味を持つC++ 構造に解決されることです。

これを回避するには、次のことを試してください。

struct blah
{
   void func()
   {
      const size_t mySize = sizeof(*this);
      BOOST_STATIC_ASSERT(mySize);
   }
};
于 2013-06-28T22:44:24.020 に答える