Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> templates should never be an answer to "my type is too long to type out"

Eh, in some cases it's a wash.

    std::vector<std::unique_ptr<mynamespace::MyWidget>> objects;
    // ...
    auto it = std::find_if(objects.begin(), objects.end(), [](auto& widget) { return widget->x == 1; });
Sure, you could repeat the type `std::unique_ptr<mynamespace::MyWidget>` in the lambda, but that's just noise. You don't spell out the types like that either in, say, C#. Yes, compilation time is an epsilon slower, but that consideration loses to readability any time of day. (Otherwise you could just remove all comments and indentation from your code - that also makes compilation faster!)


When the definition is far from the use of auto, that's exactly the case where I think repeating the type is meaningful.

Yes, it's noise to write:

    std::unique_ptr<MyWidget> widget = std::make_unique<MyWidget>();
But I don't think it's so noisy in the case you've provided. If I encountered that code, I'd wonder whether that operator-> is safe, or if you're inadvertently dropping some qualifier (e.g. is it actually a raw pointer?).

And as I suggested, it's not much harder to add a using decl:

    using UniqueWidget = std::unique_ptr<mynamespace::MyWidget>;
    // ...
    std::vector<UniqueWidget> objects;
    // ...
    auto it = std::find_if(objects.begin(), objects.end(), [](UniqueWidget&) { return widget->x == 1; });
(I also recognize that my opinion is very heavily flavored by the Google style guide: https://google.github.io/styleguide/cppguide.html#Type_deduc...)


Is it really slower ? The compiler has to compute the type of the right hand side in any case. It just has one less computation to do now (type checking the conversion to the left hand type)


I am fairly certain template expansion and skipping over whitespace while parsing are two entirely orthogonal things. using a template might make compilation an "epsilon" slower in one instance of such a use, but then those epsilons add up...


auto is an evil temptation that we've gave ourselves easily in.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: