返回值 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;
}