Live data from Hacker News

C++ braced initializers and type deduction

scottmeyers.blogspot.ca

31–38 of 38 posts

Re: C++ braced initializers and type deduction

#31

the way i handle c++ complexity is to limit its feature usage to exercise its strength letting it help me solve the problem. c++ is a big language with big grammar, as such offers a lot of different ways to say the same thing. each different construct is understood by a different set of code-readers, same way as in a complex human language. commenting c++ code is part of writing c++ due to this complexity, in my prac…

> c++ is a big language with big grammar, as such offers a lot of different ways to say the same thing

This is what I hate about Perl, so why do I like C++?

I'm quite willing to admit I'm irrational, but it feels like there's a big difference between the "there's more than one way to do things" style of both languages.

Re: C++ braced initializers and type deduction

#32
post #15

Only slightly related, but I feel (I do not yet have the experience) that 'auto' is going to make the lives of those reading code 10x harder, for the benefit of those writing code to have to think and type tiny amounts less, and for the writers of template libraries to have to think 10 times less. Especially that last part concerns me great amounts. I'm not looking forward to the first time I will have to work myself…

Many, many languages already have the equivalent. It doesn't ruin code comprehensibility in any of them, and is generally considered a net positive.

If C++ is too complex for it to work, I'm just going to consider that a strike against C++ and move on. Chalk it up as another piece of evidence that C++ is wildly more complicated than Haskell.

Re: C++ braced initializers and type deduction

#33
post #21

Earlier quoted context omitted.

Honestly, writing safe C++ code means just following a few rules of thumb. Most importantly: never do pointer arithmetic, always use smart pointers. Basically, 99% of your work should use only about 10% of the feature set of C++ explicitly. The vast majority of the time, you shouldn't even write templates (just use the ones in the STL and project-specific ones that make sense). But it's really nice that those other 9…

Their should be a guide about how to write modern and unclutered C++ without the 90% of it.

And a compiler that only accepts the 10%.

Re: C++ braced initializers and type deduction

#34
post #26
post #25

Earlier quoted context omitted.

Yes, certainly as you say, which is what concerns me - the notion that types are something that is best buried, by an influx of new users. And certainly - the case of auto x = T(); is not too bad. It's when the type is determined by template overload, or Koenig lookup, or template traits, that it becomes murky. It will take years for tool to catch up - now when I put my cursor on a variable, it will show me the type…

the notion that types are something that is best buried that was not really my claim though - types should not be buried in C++. Either the context should be clear enough to know what type it is. Or, like for lots of template code, the type simply should not matter. Obviously in those cases auto shines. In other words: if the code is written properly it shouldn't matter if auto is used or not. (and in my experience i…

No I realize you're not saying that, I'm afraid that some people will come to C++ thinking that, that was what I was trying to say.

Visual Studio with Visual Assist is what I'm using - I love it but in the future the only way to deduce some types is by compiling code, I'm afraid.

Re: C++ braced initializers and type deduction

#35
post #19
post #15

Only slightly related, but I feel (I do not yet have the experience) that 'auto' is going to make the lives of those reading code 10x harder, for the benefit of those writing code to have to think and type tiny amounts less, and for the writers of template libraries to have to think 10 times less. Especially that last part concerns me great amounts. I'm not looking forward to the first time I will have to work myself…

(C++ beginner here) I tend to use the actual type for simple enough types, typdef those template instantiations I use frequently and use auto more or less only for iterators (which are horrible to write otherwise, especially for things like map >).

It's for iterators, and similar template-heavy types, that you need them the most. Well not for an iterator over an std::vector obviously, but the worse the amount of typing for the type gets, the more you need to know the actual type to save you from having to do the type deduction in your head every time. (well not 'you' in the sense of 'the author', the whole problem it's that it's the people who need to read the code afterwards need the full type the most, the incentives are so misaligned).

I guess the more I think about it, that that's what scares me the most about 'auto' - instead of having the original author do the type deduction once (which is something he intended in the first place, hopefully), you now (as a maintainer) have to go chase it down, just to save the original author from typing out a hairy type.

Now I do realize the real use case for 'auto' - it's the cases where you don't actually know the type yet, because you're providing an extensible framework through template specialization. I have written quite a bit of such code myself. And yet, despite having suffered through all the 5-line typedefs that that sometimes required, I'd be willing to keep doing that to save myself from code by people who mistake 'auto' for a convenient way to save a few keystrokes (this is not meant as a jab at the parent, I'm just talking in general, in case anyone would interpret me wrong).

Re: C++ braced initializers and type deduction

#36
post #15

Only slightly related, but I feel (I do not yet have the experience) that 'auto' is going to make the lives of those reading code 10x harder, for the benefit of those writing code to have to think and type tiny amounts less, and for the writers of template libraries to have to think 10 times less. Especially that last part concerns me great amounts. I'm not looking forward to the first time I will have to work myself…

OTOH, with auto the reader never have to worry about narrowing conversions. And any decent IDE should show the deduced type for an auto variable on demand.

"And any decent IDE should show the deduced type for an auto variable on demand."

Eh... That can only be done by compiling the code in the background, while you're typing. Which is possible, of course, but it sets a very high barrier for tools. It will mean that only the top-3 of IDE's will provide this functionality, and even then, it will take them years to get it working well.

Re: C++ braced initializers and type deduction

#37
post #11

Earlier quoted context omitted.

Same here. C11 has driven me into mobile app development with Java and Objective C. And that's after 10+ years of C++ experience and most people consider me a C++ expert.

Do you mean C++11, rather than C11?

Yes.

Re: C++ braced initializers and type deduction

#38
post #35
post #19

Earlier quoted context omitted.

(C++ beginner here) I tend to use the actual type for simple enough types, typdef those template instantiations I use frequently and use auto more or less only for iterators (which are horrible to write otherwise, especially for things like map >).

It's for iterators, and similar template-heavy types, that you need them the most. Well not for an iterator over an std::vector obviously, but the worse the amount of typing for the type gets, the more you need to know the actual type to save you from having to do the type deduction in your head every time. (well not 'you' in the sense of 'the author', the whole problem it's that it's the people who need to read the…

Auto is useful to reduce the amount of typing and DRY principle. Your arguments are meaningless when you can hover the mouse on the "auto" keyword and have Visual Studio/Eclipse tell you its type.
Post reply on HN