在C ++模板中使用尖括号引入的所有语法问题是什么? [英] What are all the syntax problems introduced by the usage of angle brackets in C++ templates?

查看:554
本文介绍了在C ++模板中使用尖括号引入的所有语法问题是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++模板中,用尖括号 vector< int> 实例化,Java和C#语言对它们的泛型采用相同的语法。



然而,D的创造者对尖括号带来的问题非常有发言权,他们提出了一个新的语法 foo!(int)



其中之一是用另一个模板实例化一个模板 vector< vector< int> ,这将导致一些(较旧的)编译器将后面的>> >>与位移位或流操作符混淆。解决方案是在两个尖括号之间插入一个空格,但是没有编译器能够解析这个语法,现在呢?



另一个问题是当使用greater-比操作符 foo< 3> 2> 。解析器将认为运算符实际上关闭模板实例化;修复是引入括号 foo<(3> 2)> 。但我不认为有很多情况下,你需要这样做,无论如何,我宁愿在需要时键入额外的括号,而不是引入新的语法和总是

使用尖括号还有什么其他问题使得D开发人员创建新的语法?



b
$ b

当然。但它不是微不足道。特别是,它阻止您在上下文不知道的词法分析器和解析器之间实现一个干净的分离。这对于需要解析C ++,但不想/可以实现一个完全成熟的句法分析器的语法高亮显示器和其他支持工具特别难过。



它使C ++所以很难解析,很多工具根本不会打扰。这是生态系统的净损失。换句话说,它使得开发解析工具更加昂贵。



例如,对于某些模板定义, ctags ,这使它无法使用我们当前的C ++项目。很讨厌。


但我不认为有很多情况下你需要[区分尖括号和小于] / p>

这样做并不重要多久你的解析器仍然需要处理这个问题。



D决定掉角度背包是一个没有脑子的。任何一个原因都会足够,因为这是一个净收益。


In C++ templates are instantiated with angle brackets vector<int> and the Java and C# languages have adopted the same syntax for their generics.

The creators of D, however, have been quite vocal about the problems that angle brackets bring and they made a new syntax foo!(int) — but I've never seen too many details about what problems angle brackets bring, exactly.

One of them was when instantiating a template with another template vector<vector<int>>, which would cause some (older?) compilers to confuse the trailing '>>` with the bit-shift or streaming operators. The solution was to insert a space between the two angle brackets, but haven't compilers become able to parse that syntax, nowadays?

Another problem was when using the greater-than operator foo<3 > 2>. The parser would think that the operator actually closes the template instantiation; the fix was to introduce parentheses foo<(3 > 2)>. But I don't think there that many cases where you need to do this and, at any rate, I'd rather have to type the extra parentheses when they are needed, instead of introducing new syntax and always having to type the exclamation mark.

What other problems are there with angle brackets that made the D developers create a new syntax?

解决方案

but haven't compilers become able to parse that syntax, nowadays?

Of course. But it’s far from trivial. In particular, it prevents you from implementing a clean separation between context-unaware lexer and parser. This is particularly irksome for syntax highlighters and other support tools that need to parse C++, but don’t want/can implement a fully-fledged syntactical analyser.

It makes C++ so much harder to parse that a lot of tools simply won’t bother. This is a net loss for the ecosystem. Put differently: it makes developing a parsing tool much more expensive.

For instance, ctags fails for some template definitions, which makes it unusable with our current C++ project. Very annoying.

But I don't think there that many cases where you need to [distinguish between angle brackets and less-than]

It doesn’t matter how often you need to do this. Your parser still needs to handle this.

D’s decision to drop angle backets was a no-brainer. Any one reason would have sufficed, given that it’s a net benefit.

这篇关于在C ++模板中使用尖括号引入的所有语法问题是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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