傳回值 decltype(表達式)
[傳回值的類型是表達式參數的類型]
這個可也用來決定表達式的類型,就像Bjarne暗示的一樣,如果我們需要去初始化某種類型的變量,auto是最簡單的選擇,但是如果我們所需的類型不是一個變量,例如傳回值這時我們可也試一下decltype。
現在我們回看一些例子我們先前做過的,
template <class U, class V>
void Somefunction(U u, V v)
{
result = u*v;//now what type would be the result???
decltype(u*v) result = u*v;//Hmm .... we got what we want
}
在下面的一個段落我将會讓你熟悉這個觀念用 auto 和 decltype 來聲明模闆函數的傳回值,其類型依靠模闆參數。
1. 如果這個表達式是個函數,decltype 給出的類型為函數傳回值的類型。
int add(int i, int j){ return i+j; }
decltype(add(5,6)) var = 5;//Here the type of var is return of add() -> which is int
2.如果表達式是一個左值類型,那麼 decltype 給出的類型為表達式左值引用類型。
struct M { double x; };
double pi = 3.14;
const M* m = new M();
decltype( (m->x) ) piRef = pi;
// Note: Due to the inner bracets the inner statement is evaluated as expression,
// rather than member 'x' and as type of x is double and as this is lvale
// the return of declspec is double& and as 'm' is a const pointer
// the return is actually const double&.
// So the type of piRef is const double&
3.非常重要的标記一下,decltype 不會執行表達式而auto會,他僅僅推論一下表達式的類型。
int foo(){}
decltype( foo() ) x; // x is an int and note that
// foo() is not actually called at runtime
跟蹤傳回類型:
這對 C++ 開發者來說是一個全新的特性,直到現在函數的傳回類型必須放在函數名的前面。到了 C++11,我們也可以将函數傳回值的類型放在函數聲明後,當然僅需要用 auto 替代傳回類型。現在我們想知道怎麼做,讓我們來尋找答案:
template<class U, class V>
??? Multiply(U u, V v) // how to specifiy the type of the return value
{
return u*v;
}
我們明顯的不能像這樣:
template<class U, class V>
decltype(u*v) Multiply(U u, V v) // Because u & v are not defined before Multiply.
// What to do...what to do !!!
{
return u*v;
}
這種情況我們可也使用 auto 然後當我們使用 decltype(u*v) 作為傳回值這個類型便知曉了.
這是不是很酷?
template<class U, class V>
auto Multiply(U u, V v) -> decltype(u*v) // Note -> after the function bracet.
{
return u*v;
}
/*
* cppprimer.cpp
*
* Created on: 2015.3.7
* Author: JDX
*/
/*eclipse cdt, gcc 4.8.1*/
#include <QCoreApplication>
#include <iostream>
#include <vector>
#include <string>
#include <type_traits>
#include <typeinfo>
using namespace std;
/*需要顯示定義傳回類型*/
template <typename T1, typename T2, typename T3>
T1 sum (T2, T3)
{
T2 i2; T3 i3;
return static_cast<T1>(i2+i3);
}
/*注意拖尾傳回類型(trailing return type)前面需要加auto*/
template <typename It>
auto fcn(It beg, It end) -> decltype(*beg)
{
return *beg;
}
/*注意拖尾傳回類型是值, 注意第二個typename, 表明為類型*/
template <typename It>
auto fcn2(It beg, It end) ->
typename remove_reference<decltype(*beg)>::type
{
return *beg;
}
int main (void)
{
std::cout << "The type of sum return : " <<
typeid(sum<float>(2, 3)).name() << std::endl;
std::vector<int> vi = {1, 2, 3, 4, 5};
std::vector<std::string> vs = {"girl", "lady"};
/*傳回的是引用*/
auto &i = fcn(vi.begin(), vi.end());
auto &s = fcn(vs.begin(), vs.end());
i = 12, s = "woman";
std::cout << "*vi.begin() = " << *vi.begin() << std::endl;
std::cout << "*vs.begin() = " << *vs.begin() << std::endl;
/*傳回的是值, 是以不能在使用&符号*/
auto i2 = fcn2(vi.begin(), vi.end());
auto s2 = fcn2(vs.begin(), vs.end());
std::cout << "i2 = " << i2 << std::endl;
std::cout << "s2 = " << s2 << std::endl;
return 0;
}