爲了在運行期減小應類型安全而致使的程序崩潰,咱們回使用斷言和一些模板元編程來進行編譯期的類型檢測。node
判斷一個類型是不是POD(plain old data 的縮寫,一個普通的古老的數據結構(POD)是一種數據結構。它僅做爲被動的收藏的字段值,不使用封包或者otherobject-oriented特徵。) 編程
template <typename T> struct must_be_pod { must_be_pod() { void (*p)() = constraints; } static void constraints() { union { T T_is_not_pod; }; } };class node { public: node() {} int operator[](int index) const { return 0; } }; int main() { // must_have_base<father,children> a; // must_be_subscriptable<char[]> a; must_be_pod<node> a; //must_be_subscriptable_as_decayable_pointer<int*> a; cout << "Hello World!" << endl; return 0; }