• Home
  • Welcome to the Jungle
  • Elements of Modern C++ Style
  • About

Sutter’s Mill

Herb Sutter on software, hardware, and concurrency

Feeds:
Posts
Comments
« Reader Q&A: When will better JITs save managed code?
Reader Q&A: What does it mean for [[attributes]] to affect language semantics? »

Reader Q&A: auto and expression templates

2012-04-03 by Herb Sutter

Motti asked:

While you’re dealing with reader’s Qs….

In your keynote in “Going Native” you mentioned that type inference should almost always be used, except for some obscure cases with expression templates.

Yes. To give people context, the idea is when declaring local variables, prefer to use auto to deduce the type. For example:

auto x = begin(v);

This seems like a rather serious wart on the language,

It’s a wart, but I don’t know about “serious” – it doesn’t come up that often. Still, your question is quite apropos:

part of the power of expression templates (to my understanding) is that they can be dropped in by a library implementer and thus improve the clients’ code without their knowledge.

Was there any discussion to allow type authors to opt-out of type inference? (e.g. by allowing an “operator auto()”). If this wasn’t discussed for C++11 is it being discussed for C++1y?

Yes, and even exactly that spelling has been suggested. I’ll take that as a +1 for discoverability if we name it that!

(For people reading this comment, if it doesn’t make any sense I wrote about it last year in my blog http://lanzkron.wordpress.com/2011/02/21/inferring-too-much/)

Posted in C++, Reader Q&A | 9 Comments

9 Responses

  1. on 2012-04-03 at 4:05 pm jmckesson

    I think that it would be better to have a mechanism where a class states what type should be deduced if *it* is auto-deduced. So if I have some string type that uses expression templates, I can force `auto str = string_type(…) + “foo”;` to be interpreted as `string_type str = string_type(…) + “foo”;`


  2. on 2012-04-03 at 7:13 pm Soledad Alborno

    I personally think that using auto too often will reduce readability in the code. I would take it carefully.


  3. on 2012-04-03 at 7:24 pm Ld

    auto can (and will) indeed break C++03 expression templates, but with perfect-forwarding it’s now possible to write expression templates that properly ‘swallow’ temporaries such that they’re safe in the face of auto. I.e. in auto tree = a + std::string { “foo” } + c; the tree object would hold a reference to a, an std::string object with value “foo”, and a reference to c. This does mean that with something like auto more_tree = (std::string { “foo” } + a) + “bar”; there are moves (from the object with value “foo” in the temporary tree on the lhs of the last operator+ to the new object with value “foo” inside more_tree) where previously there were just some more references being created, which could be costly for a type where moves are effectively copies. But that’s somewhat premature, and the C++03 really was unsafe to begin with (the private type could already be accessed, and thus unsafely stored, through function template template).


  4. on 2012-04-04 at 11:10 am Motti Lanzkron

    @jmckesson that would not be *better*, it would be exactly what I suggested.


  5. on 2012-04-05 at 4:56 am Ivan

    Why cant we gat those small things before 2017? I mean I know it is an ISO and all that, but tbh I dont get why you cant you just standardize “quickfixes” like every 2 y or something.


  6. on 2012-04-07 at 7:53 am Nicol Bolas

    @Motti: There is a difference between “opt-out of type inference” and “*change* the type inferred.” You asked for the former, and what I suggested was the latter.

    An `operator auto()` cannot return a type, since types in C++ are not things you can “compute” without template metaprogramming. If you want to change what type is inferred, then you need to have some kind of construct that resolves to an actual type.


  7. on 2012-04-07 at 12:11 pm Motti Lanzkron

    @Nicol Perhaps I gave the wrong impression when I said “opt out”, obviously you need a type for “auto” to deduce, my suggestion (as described in the post linked to above) was to have operator auto have a return type.

    e.g.

    string operator auto() const
    {
    return stringify();
    }


  8. on 2012-04-09 at 10:37 am Michael Winter

    Is there any reason why changing the definition of “operator string() const” to “operator string() &&”, would not work? Since that doesn’t seem to have been brought up, I’m guessing it’s for an obvious reason that I’ve just missed, aside from “most current compilers don’t support ‘rvalue references to *this’ “.

    It breaks type-deduction, which is unfortunate, but it seemingly prevents accidental undefined behavior, which is nice.

    Also, clang complains about “operator string() const &&” if the type of the object isn’t itself const, is that a defect in clang (or at least the random snapshot I happen to have)?


  9. on 2012-04-30 at 8:36 am Chip

    @Ivan has a good question. Why make the only cycle ten years long?



Comments are closed.

  • Tweets

    • GotW #4: Class Mechanics (7/10): How good are you at the details of writing classes? This item focuses not onl... bit.ly/10TmyVQ 3 days ago
    • GotW #3 Solution: Using the Standard Library (or, Temporaries Revisited): Effective reuse is an important part... bit.ly/19zU1Uo 3 days ago
    • GotW #3: Using the Standard Library (or, Temporaries Revisited) (3/10): Effective reuse is an important part o... bit.ly/13TRX9o 6 days ago
    Follow @herbsutter
  • Popular

    • GotW #3 Solution: Using the Standard Library (or, Temporaries Revisited)
    • GotW #4: Class Mechanics (7/10)
    • GotW #1 Solution: Variable Initialization – or Is It?
  • Categories

    • Apple
    • C# / .NET
    • C++
    • Cloud
    • Concurrency
    • Effective Concurrency
    • Friday Thoughts
    • GotW
    • Hardware
    • Java
    • Microsoft
    • Opinion & Editorial
    • Reader Q&A
    • Software Development
    • Talks & Events
    • Uncategorized
    • Web

Blog at WordPress.com.

Theme: Customized MistyLook by WPThemes.


Follow

Get every new post delivered to your Inbox.

Join 1,389 other followers

Powered by WordPress.com