什么时候应该使用C ++ 14自动返回类型的扣除? [英] When should I use C++14 automatic return type deduction?

查看:173
本文介绍了什么时候应该使用C ++ 14自动返回类型的扣除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用GCC 4.8.0发布,我们有一个支持自动返回类型推导的编译器,它是C ++ 14的一部分。使用 -std = c ++ 1y ,我可以这样做:

With GCC 4.8.0 released, we have a compiler that supports automatic return type deduction, part of C++14. With -std=c++1y, I can do this:

auto foo() { //deduced to be int
    return 5;
}

我的问题是:我应该何时使用此功能?

My question is: When should I use this feature? When is it necessary and when does it make code cleaner?

我可以想到的第一个场景是可能的。可以这样写的每个函数应该是。

The first scenario I can think of is whenever possible. Every function that can be written this way should be. The problem with this is that it might not always make the code more readable.

下一个方案是避免更复杂的返回类型。作为非常轻的示例:

The next scenario is to avoid more complex return types. As a very light example:

template<typename T, typename U>
auto add(T t, U u) { //almost deduced as decltype(t + u): decltype(auto) would
    return t + u;
}



我不相信这真的会是一个问题,

I don't believe that would ever really be a problem, though I guess having the return type explicitly depend on the parameters could be clearer in some cases.

接下来,以防止冗余:

auto foo() {
    std::vector<std::map<std::pair<int, double>, int>> ret;
    //fill ret in with stuff
    return ret;
}



在C ++ 11中,我们有时可以 return {5,6,7}; 代替向量,但并不总是能够实现,我们需要在函数头和函数体中指定类型。

In C++11, we can sometimes just return {5, 6, 7}; in place of a vector, but that doesn't always work out and we need to specify the type in both the function header and the function body. This is purely redundant, and automatic return type deduction saves us from that redundancy.

最后,它可以用来代替非常简单的函数:

Finally, it can be used in place of very simple functions:

auto position() {
    return pos_;
}

auto area() {
    return length_ * width_;
}

有时候,我们可能看一下函数,想知道类型,如果它没有提供,我们必须去代码中的另一点,如 pos _ 被声明。

Sometimes, though, we might look at the function, wanting to know the exact type, and if it isn't provided there, we have to go to another point in the code, like where pos_ is declared.

在这些情况下,哪些实际上被证明是一种情况,这个特性对于使代码更清洁是有用的?我在这里忽略了什么情况?在使用此功能之前,我应该采取什么预防措施,以便它以后不咬我?有没有什么新的这个功能带来的表是不可能没有它?

With those scenarios laid out, which of them actually prove to be a situation where this feature is useful in making the code cleaner? What about scenarios I have neglected to mention here? What precautions should I take before using this feature so that it doesn't bite me later? Is there anything new this feature brings to the table that isn't possible without it?

请注意,多个问题是为了帮助查找透视从中可以回答这个问题。

推荐答案

C ++ 11引发了类似的问题:何时使用返回类型扣除lambdas以及何时使用 auto 变量。

C++11 raises similar questions: when to use return type deduction in lambdas, and when to use auto variables.

C和C ++中的问题的传统答案03已经跨语句边界我们使类型显式,在表达式中他们通常是隐式的,但我们可以使他们显式使用转换。 C ++ 11和C ++ 1y引入了类型推导工具,以便您可以在新的地方放弃类型。

The traditional answer to the question in C and C++03 has been "across statement boundaries we make types explicit, within expressions they are usually implicit but we can make them explicit with casts". C++11 and C++1y introduce type deduction tools so that you can leave out the type in new places.

对不起,但你不会解决这是通过制定一般规则。你需要查看特定的代码,并自己决定它是否有助于可读性指定类型的所有地方:是更好的你的代码说,这个东西的类型是X,或者是更好的你的代码说,这个东西的类型与理解这部分代码是无关的:编译器需要知道,我们可能可以工作,但我们不需要在这里说?

Sorry, but you're not going to solve this up front by making general rules. You need to look at particular code, and decide for yourself whether or not it aids readability to specify types all over the place: is it better for your code to say, "the type of this thing is X", or is it better for your code to say, "the type of this thing is irrelevant to understanding this part of the code: the compiler needs to know and we could probably work it out but we don't need to say it here"?

由于可读性没有被客观地定义[*],并且由于阅读器的不同,你有责任作为一段代码的作者/编辑,不能完全满足风格指南。即使在风格指南确实指定规范的范围内,不同的人也会喜欢不同的规范,并且倾向于发现不太可读的不熟悉的东西。因此,通常只能在其他样式规则的上下文中判断特定建议的样式规则的可读性。

Since "readability" is not objectively defined[*], and furthermore it varies by reader, you have a responsibility as the author/editor of a piece of code that cannot be wholly satisfied by a style guide. Even to the extent that a style guide does specify norms, different people will prefer different norms and will tend to find anything unfamiliar to be "less readable". So the readability of a particular proposed style rule can often only be judged in the context of the other style rules in place.

所有的场景(即使是第一个)找到用于某人的编码风格。我个人认为第二个是最引人注目的用例,但即使如此,我预计它将取决于您的文档工具。查看记录表明函数模板的返回类型是 auto ,而看到它被记录为 decltype(t + u)创建一个可以(希望)依赖的发布接口。

All of your scenarios (even the first) will find use for somebody's coding style. Personally I find the second to be the most compelling use case, but even so I anticipate that it will depend on your documentation tools. It's not very helpful to see documented that the return type of a function template is auto, whereas seeing it documented as decltype(t+u) creates a published interface you can (hopefully) rely on.

[*]偶尔有人试图做一些客观的测量。在很小程度上,任何人提出任何具有统计意义和普遍适用的结果,他们完全被工作程序员忽略,赞成作者的本能是什么是可读。

[*] Occasionally someone tries to make some objective measurements. To the small extent that anyone ever comes up with any statistically significant and generally-applicable results, they are completely ignored by working programmers, in favour of the author's instincts of what is "readable".

这篇关于什么时候应该使用C ++ 14自动返回类型的扣除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆