在函数被调用之前如何推断auto? [英] How do I deduce auto before a function is called?

查看:501
本文介绍了在函数被调用之前如何推断auto?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用函数返回类型演绎

  auto func(); 

int main(){func(); }

auto func(){return 0; }

错误:在扣除'auto'之前使用'auto func()'



有没有办法使用这个特性,而不需要在调用之前指定定义?使用大型调用树时,重新安排函数会变得很复杂,以便在调用它们的所有位置之前看到它们的定义。当然,一个评估可能会被搁置,直到找到一个特定的函数定义,然后可以推导出auto。

解决方案

即使忽略了实际问题(需要多遍编译,通过相互递归类型定义轻松地创建不可判断的返回类型,当所有事物都被分离时,难以隔离编译错误的来源解决方案等)和设计问题(即前向声明几乎没有用处),C ++ 11的设计易于实现。因为编写编译器比较困难,所以需要强有力的证明。



auto 的无数限制意味着它是真的很容易将其融入到现有的编译器中:它是我的经验中支持最多的C ++ 11功能之一。 C ++ 14放宽了许多限制,但几乎没有你描述的那么远。每次放松都需要证明和确信编译器编写人员实现这些代码是值得的。

我现在甚至不想使用该功能,因为我喜欢签名的功能在我称他们的时候至少可以被扣除。


while experimenting with function return type deduction

auto func();

int main() { func(); }

auto func() { return 0; }

error: use of ‘auto func()’ before deduction of ‘auto’

Is there a way to use this feature without needing to specify the definition before the call? With a large call tree, it becomes complicated to re-arrange functions so that their definition is seen before all of the places they are called. Surely an evaluation could be held off until a particular function definition was found and auto could then be deduced.

解决方案

No, there is not.

Even ignoring the practical problems (requiring multi-pass compilation, ease of making undecidable return types via mutually recursive type definitions, difficulty in isolating source of compilation errors when everything resolves, etc), and the design issues (that forward declaration is nearly useless), C++11 was designed with ease of implementation in mind. Things that made it harder to write a compiler needed strong justification.

The myriad restrictions on auto mean that it was really easy to slide it into existing compilers: it is among the most supported C++11 features in my experience. C++14 relaxes many of the restrictions, but does not go nearly as far as you describe. Each relaxation requires justification and confidence that it will be worth the cost to compiler writers to implement.

I would not even want that feature at this time, as I like the signatures of my functions to be deducible at the point I call them, at the very least.

这篇关于在函数被调用之前如何推断auto?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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