函數模板ios
std::declval (c++11 only)c++
template<typename T>函數
typename add_rvalue_reference<T>::type declval() noexcept;c++11
功能描述:對象
返回一個類型的右值引用,不論是否有沒有默認構造函數或該類型不能夠建立對象。(能夠用於抽象基類);blog
參數:ci
無it
返回值:io
類型T的右值引用模板
例子:
// declval example #include <utility> // std::declval #include <iostream> // std::cout struct A { // abstract class virtual int value() = 0; }; class B : public A { // class with specific constructor int val_; public: B(int i,int j):val_(i*j){} int value() {return val_;} }; int main() { decltype(std::declval<A>().value()) a; // int a decltype(std::declval<B>().value()) b; // int b decltype(B(0,0).value()) c; // same as above (known constructor) a = b = B(10,2).value(); std::cout << a << '\n'; return 0; }