Re: [boost] C++11 Metaprogramming

On 06/04/12 09:04, Andrzej Krzemienski wrote:
And this is the worst part: I forgot the "typename". Having to type this "typename" is really terrible and just scares off many people. If you do not mind it, it only means that you got used to it; but people should not be forced to get used to too many gotchas.
The language requires to specify whenever dependent names in template contexts are anything else than values (i.e. types or templates).
There is no substitute for learning the basics of the language. This is not a gotcha, just normal usage of templates.
Clearly, if you do not find requiring of the programmers to type this "typename" in these places even slightly wrong, you will not appreciate alias templates as a useful addition to meta-programming. Also, if you did a lot of advanced meta-programming, you have probably mastered the rules of template syntax, and find it difficult to sympathize with those that did not master the subject. But looking at it from the fresh programmer's perspective, the fact that the language requires "to specify whenever dependent names in template contexts are anything else than values" means that the language is not friendly to the fresh programmers (I understand that there are good reasons for requiring this, but still...). Based on the Wkipedia's definition of "gotcha" I interpret this typename as one. "In programming, a gotcha is a feature of a system, a program or a programming language that works in the way it is documented but is counter-intuitive and almost invites mistakes because it is both enticingly easy to invoke and completely unexpected and/or unreasonable in its outcome." It (the usage of typename) only appears as "normal usage" after you are past the stage of surprise. The context of your reply seems to imply that whoever finds this behavior surprising is guilty of not having acquired the basics of the language. Unless I misunderstood you, I think this position is a bit unfair. Regards, &rzej

----- Mail original -----
De: "Andrzej Krzemienski" <akrzemi1@gmail.com> À: boost@lists.boost.org Envoyé: Vendredi 6 Avril 2012 12:26:23 Objet: Re: [boost] C++11 Metaprogramming
On 06/04/12 09:04, Andrzej Krzemienski wrote:
And this is the worst part: I forgot the "typename". Having to type this "typename" is really terrible and just scares off many people. If you do not mind it, it only means that you got used to it; but people should not be forced to get used to too many gotchas.
The language requires to specify whenever dependent names in template contexts are anything else than values (i.e. types or templates).
There is no substitute for learning the basics of the language. This is not a gotcha, just normal usage of templates.
Clearly, if you do not find requiring of the programmers to type this "typename" in these places even slightly wrong, you will not appreciate alias templates as a useful addition to meta-programming. Also, if you did a lot of advanced meta-programming, you have probably mastered the rules of template syntax, and find it difficult to sympathize with those that did not master the subject.
But looking at it from the fresh programmer's perspective, the fact that the language requires "to specify whenever dependent names in template contexts are anything else than values" means that the language is not friendly to the fresh programmers (I understand that there are good reasons for requiring this, but still...).
Based on the Wkipedia's definition of "gotcha" (...)
(off-topic) IMHO, a gotcha leading only to a compile time error is not really a gotcha. Regards, Ivan

On 4/6/12 12:45 PM, ivan.lelann@free.fr wrote:
Clearly, if you do not find requiring of the programmers to type this "typename" in these places even slightly wrong, you will not appreciate alias templates as a useful addition to meta-programming. Also, if you did a lot of advanced meta-programming, you have probably mastered the rules of template syntax, and find it difficult to sympathize with those that did not master the subject.
But looking at it from the fresh programmer's perspective, the fact that the language requires "to specify whenever dependent names in template contexts are anything else than values" means that the language is not friendly to the fresh programmers (I understand that there are good reasons for requiring this, but still...).
Based on the Wkipedia's definition of "gotcha" (...)
(off-topic) IMHO, a gotcha leading only to a compile time error is not really a gotcha.
The problem is that VC with its non two phase lookup often doesn't require it, and compiles fine without. I can't count the times I had to fix broken check-ins from our Windows developers, who fell into exactly this gotcha. regards Fabio

On 06/04/12 12:26, Andrzej Krzemienski wrote:
But looking at it from the fresh programmer's perspective, the fact that the language requires "to specify whenever dependent names in template contexts are anything else than values" means that the language is not friendly to the fresh programmers (I understand that there are good reasons for requiring this, but still...).
Based on the Wkipedia's definition of "gotcha" I interpret this typename as one. "In programming, a gotcha is a feature of a system, a program or a programming language that works in the way it is documented but is counter-intuitive and almost invites mistakes because it is both enticingly easy to invoke and completely unexpected and/or unreasonable in its outcome."
When a compiler encounters a function or class template, it builds a parametric AST. Without knowing what the parameters are, it cannot know what the names that depend on the parameters are, and thus what kind of AST to generate; indeed C++, unlike other programming languages, makes it ambiguous what kind of entity a name refers to. Some compilers do not require this because they do not build a real AST when parsing a template. As a result they have a lot of bugs, especially in name lookup. An alternative would be to deduce whether it's a type or a value depending on the context it is used in. This could be done with a GLR parser, but I don't think any serious compiler tried to implement this.

on Fri Apr 06 2012, Mathias Gaunard <mathias.gaunard-AT-ens-lyon.org> wrote:
On 06/04/12 12:26, Andrzej Krzemienski wrote:
But looking at it from the fresh programmer's perspective, the fact that the language requires "to specify whenever dependent names in template contexts are anything else than values" means that the language is not friendly to the fresh programmers (I understand that there are good reasons for requiring this, but still...).
Based on the Wkipedia's definition of "gotcha" I interpret this typename as one. "In programming, a gotcha is a feature of a system, a program or a programming language that works in the way it is documented but is counter-intuitive and almost invites mistakes because it is both enticingly easy to invoke and completely unexpected and/or unreasonable in its outcome."
When a compiler encounters a function or class template, it builds a parametric AST. Without knowing what the parameters are, it cannot know what the names that depend on the parameters are, and thus what kind of AST to generate; indeed C++, unlike other programming languages, makes it ambiguous what kind of entity a name refers to.
Some compilers do not require this because they do not build a real AST when parsing a template. As a result they have a lot of bugs, especially in name lookup.
I think Andrzej understands very well the reasons for "typename" and the consequences of failing to implement 2-phase name lookup. IIUC he's just saying (correctly) that such understanding does not come easily---even many very experienced C++ programmers don't have it---and the current situation is hard for many programmers to deal with.
An alternative would be to deduce whether it's a type or a value depending on the context it is used in.
...which is in some cases impossible. My book has examples of code that could be valid either way. -- Dave Abrahams BoostPro Computing http://www.boostpro.com

On Fri, Apr 6, 2012 at 5:26 AM, Andrzej Krzemienski <akrzemi1@gmail.com> wrote:
Clearly, if you do not find requiring of the programmers to type this "typename" in these places even slightly wrong, you will not appreciate alias templates as a useful addition to meta-programming. Also, if you did a lot of advanced meta-programming, you have probably mastered the rules of template syntax, and find it difficult to sympathize with those that did not master the subject.
Andrzej, do not feel alone. I think I've practiced enough of the gymnastics to master the rules and the syntax; yet, I agree with what you are saying 100%. -- Gaby
participants (6)
-
Andrzej Krzemienski
-
Dave Abrahams
-
Fabio Fracassi
-
Gabriel Dos Reis
-
ivan.lelann@free.fr
-
Mathias Gaunard