當我嘗試編譯爲什麼術語不評估一個帶有0個參數的函數?
template<bool val>
struct boolean { static const bool value = val; };
template<typename T>
struct is_callable : boolean<sizeof((*(T*)0)()) >= 0> { }; // error!
int main(void) { bool b = is_callable<int (*)()>::value; }
我得到:
error C2064: term does not evaluate to a function taking 0 arguments
see reference to class template instantiation 'is_callable<T>' being compiled
我敢肯定int (*)()
是可調用與0
參數...那麼,爲什麼不這樣編?
工程在GCC 4.6.2。 – 2012-01-06 02:19:39
@KerrekSB:呵呵,這很有趣...我用VC 2010試了一下,它也給出了一個錯誤。那麼它是一個錯誤?我不知道,因爲有一個空指針被解除引用,但實際上我沒有看到它的任何錯誤... – Mehrdad 2012-01-06 02:22:48
沒有取消引用。 'sizeof'不會評估它的參數。 – 2012-01-06 02:24:30