std::declval
來自 cppreference.com
在標頭檔案 <utility> 中定義 |
||
template< class T > typename std::add_rvalue_reference<T>::type declval() noexcept; |
(C++11 起) (直到 C++14) (僅限未求值上下文) |
|
template< class T > std::add_rvalue_reference_t<T> declval() noexcept; |
(C++14 起) (僅限未求值上下文) |
|
此輔助模板用於編寫出現在未求值上下文(通常是decltype
的運算元)中的表示式。在未求值上下文,此輔助模板將任何型別T
(可以是不完整型別)轉換為該型別的一個表示式,從而可以在不需要透過建構函式的情況下使用T的成員函式。
std::declval
只能在未求值上下文中使用,並且不需要被定義;如果表示式中包含此函式被求值,則會出錯。正式地說,如果此函式被ODR 使用,則程式格式錯誤。
目錄 |
[編輯] 引數
(無)
[編輯] 返回值
不能被求值,因此從不返回值。返回型別是T&&
(應用引用摺疊規則),除非T
是(可能帶有cv限定符的)void,在這種情況下返回型別是T
。
[編輯] 注意
std::declval
常用於模板中,其中可接受的模板引數可能沒有共同的建構函式,但具有相同成員函式,且需要其返回型別。
[編輯] 可能實現
template<typename T> typename std::add_rvalue_reference<T>::type declval() noexcept { static_assert(false, "declval not allowed in an evaluated context"); } |
[編輯] 示例
執行此程式碼
#include <iostream> #include <utility> struct Default { int foo() const { return 1; } }; struct NonDefault { NonDefault() = delete; int foo() const { return 1; } }; int main() { decltype(Default().foo()) n1 = 1; // type of n1 is int decltype(std::declval<Default>().foo()) n2 = 1; // same // decltype(NonDefault().foo()) n3 = n1; // error: no default constructor decltype(std::declval<NonDefault>().foo()) n3 = n1; // type of n3 is int std::cout << "n1 = " << n1 << '\n' << "n2 = " << n2 << '\n' << "n3 = " << n3 << '\n'; }
輸出
n1 = 1 n2 = 1 n3 = 1
[編輯] 參閱
decltype 說明符(C++11) |
獲取表示式或實體的型別 |
(C++11)(C++20 中已移除)(C++17) |
推導呼叫可呼叫物件與一組引數的結果型別 (類模板) |