http://en.cppreference.com/w/cpp/utility/functional/function
operator bool
被描述: 「所存儲的可調用對象檢查是否是有效的」。
推測默認構造std::function
是無效的,但這是唯一的情況嗎?
另外,它是如何檢查它是否有效?
operator()
在std::bad_function_call
正好是對象無效的情況下是什麼情況?
http://en.cppreference.com/w/cpp/utility/functional/function
operator bool
被描述: 「所存儲的可調用對象檢查是否是有效的」。
推測默認構造std::function
是無效的,但這是唯一的情況嗎?
另外,它是如何檢查它是否有效?
operator()
在std::bad_function_call
正好是對象無效的情況下是什麼情況?
它寫得不好,你的困惑是有道理的。通過「有效」他們的意思是「有一個目標」。
一個std::function
「有目標」,當它被賦予的功能:
std::function<void()> x; // no target
std::function<void()> y = some_void_function; // has target
x = some_other_void_function; // has target
y = nullptr; // no target
x = y; // no target
他們應該有他們要麼使用它之前,或者乾脆堅持與官方措辭定義的「有效」。
語言標準說
explicit operator bool() const noexcept;
返回:如果*這有一個目標,否則爲false。
這意味着function
有任何要調用。默認構建的function
顯然沒有。
好吧,所以檢查有效性只是類型檢查,並調用一個默認構建的'std :: function'恰好是'std :: bad_function_call'被引發的情況,yup? – dpj 2012-08-09 16:47:05
@ user710408:我不知道你的意思是「有效性檢查只是類型檢查」。有效性(現在假設定義「有目標」)在運行時確定,類型檢查在編譯時發生。並且函數調用無效(沒有目標),是默認構造或被明確指定爲'nullptr'的調用。 – GManNickG 2012-08-09 16:55:05
我不認爲我知道我的意思!謝謝:) – dpj 2012-08-09 17:07:59