decltype和新的返回值语法

新的返回值语法

让我们讲一下新的返回值语法,这个语法还能看到auto的另一个用处。在以前版本的C和C++中,返回值的类型必须写在函数的前面:

int multiply(int x, int y)  

在C++11中,你可以把返回类型放在函数声明的后面,用auto代替前面的返回类型,像这样:

auto multiply(int x, int y) -> int  

但是为什么我要这样用?让我们看一个证明这个语法好处的例子。一个包含枚举的类:

class Person
{
public:
    enum PersonType { ADULT, CHILD,  SENIOR} ;
    void setPersonType(PersonType person_type);
    PersonType getPersonType();

private:
    PersonType _person_type;
};

我们写了一个简单的类,里面有一个类型PersonType表明Person是小孩、成人和老人。不做特殊考虑,我们定义这些成员方法时会发生什么? 第一个设置方法,很简单,你可以使用枚举类型PersonType而不会有错误:

void Person::setPersonType(PersonType person_type)
{
    _person_type = person_type;
}

而第二个方法却是一团糟。简单的代码却编译不过:

PersonType Person::getPersonType()
{
    return _person_type;
}

你必须要这样写,才能使返回值正常工作

Person::PersonType Person::getPersonType()
{
    return _person_type;
}

这可能不算大问题,不过会容易出错,尤其是牵连进模板的时候。

这就是新的返回值语法引进的原因。因为函数的返回值出现在函数的最后,而不是前面,你不需要补全类作用域。当编译器解析到返回值的时候,它已经知道返回值属于Person类,所以它也知道PersonType是什么。

auto Person::getPersonType() -> PersonType
{
    return _person_type;
}  

好,这确实不错,但它真的能帮助我们什么吗?我们还不能使用新的返回值语法去解决我们之前的问题,我们能吗?不能,让我们介绍新的概念:decltype。

decltype

decltype是auto的反面兄弟。auto让你声明了一个指定类型的变量,decltype让你从一个变量(或表达式)中得到类型。我说的是什么?

int x = 100;
decltype(x) y = x;  

可以对基本上任何类型使用decltype,包括函数的返回值。嗯,听起来像个熟悉的问题,假如我们这样写(上一篇auto例子):

decltype(builder.makeObject())  

我们将得到makeObject的返回值类型,这能让我们指定makeAndProcessObject的返回类型。我们可以整合进新的返回值语法:

template <typename BuiltType, typename Builder>
auto makeAndProcessObject(cosnt Builder& builder) -> decltype(builder.makeObject())
{ 
    BuiltType val = builder.makeObject(); 
    
    return val;
}  

这仅适用于新的返回值语法,因为旧的语法下,我们在声明函数返回值的时候无法引用函数参数,而新语法,所有的参数都是可访问的。

原文博客: http://towriting.com/blog/2013/08/08/improved-type-inference-in-cpp11/

原文地址:https://www.cnblogs.com/ThatsMyTiger/p/7085309.html