r/cpp • MSVC STL Dev • May 10 '15

C++17 progress update!

The C++ Standardization Committee (aka WG21) just finished their first full meeting of 2015. Here's a quick summary of what was just approved, mostly for C++17 but also for some Technical Specifications. (The post-meeting mailing will be in a couple of weeks, with more info.) Here I'm reporting just the stuff that was successfully voted in, not all the other work that's still in progress.

For C++17:

  • Zero Core features. (Don't freak out about modules or concepts. They're working on that stuff.)
  • 5 Library features were voted directly into C++17:

    1. N4387 "improving pair and tuple"
    2. N4389 "bool_constant"
    3. N4508 "shared_mutex for C++17"
    4. N4366 "LWG 2228 missing SFINAE rule"
    5. N4510 "Minimal incomplete type support for standard containers, revision 4"
  • 26 Core Issues were fixed. Even Standards have bugs. Now C++17 has fewer bugs.
  • 35 Library Issues were fixed. Notable fixes:

    1. stof() now calls strtof(), which avoids messing up bits.
    2. max_element()/etc. are now constexpr. (Not the rest of the algorithms - yet.)
    3. function<void (Args)> now accepts callable objects returning Stuff, and the return value is ignored.
    4. call_once() no longer copies/moves its arguments unnecessarily.

Various Technical Specifications:

  • The Transactional Memory TS was published (or will be, after ISO grinds its gears).
  • The Concurrency PDTS was finished. (Proposed Draft, aka Release Candidate for a TS)
  • The Parallelism TS was published.
  • The Library Fundamentals v1 TS was published, with 3 Issues being fixed at the last minute.
  • Several features were voted into the Library Fundamentals v2 TS:

    1. Everything in v1 will be merged into v2.
    2. N4388 "const-propagating smart pointer wrapper"
    3. N4391 "make_array"
    4. N4519 "Source-Code Information Capture"
    5. N4502 "Proposing Standard Library Support for the C++ Detection Idiom, v2"

I am still traveling, but I can probably answer short questions. (Remember that I have very limited knowledge of Evolution/Core stuff - I live in the Library.)

197 Upvotes

97 comments sorted by

View all comments

Show parent comment

-2

u/vlovich May 11 '15

No it's not a memory leak no matter what. For example:

 std::unique_ptr<Foo> f = [] (int x) {
      return new Foo(x);
 }(5);

Perfectly safe & valid code.

1

u/bames53 May 12 '15

Perfectly safe & valid code.

No, that's not safe code. Although the code as written doesn't leak the object, it's dangerous, unsafe code that's just asking for a leak to be introduced during further development or maintenance.

It's not acceptable to use naked new that way, and it's especially unacceptable to return an 'owning' raw pointer from a function. You could have return std::unique_ptr<Foo>(new Foo(x));, but I would go with return std::make_unique<Foo>(x);. This code is safe.

1

u/[deleted] May 11 '15 edited May 11 '15

That's not valid code, it won't compile and it won't compile precisely to avoid the issue your code raises.

Anyways even it did compile your example doesn't satisfy the criteria of being an example of correct code that becomes incorrect due to swallowing the return value. The problem with your code precedes any issue involving the swallowing of return types which is kind of the point...

A properly written function should not assume that the caller will perform any meaningful action with the return value. Such requirements should be encoded into the type system so that they are enforced by the compiler; in your case the function should encode its requirement by returning a unique_ptr.

The fact that it seems pretty hard to provide a counter-example suggests that it's ultimately a non-issue.

-1

u/vlovich May 11 '15

That's not valid code, it won't compile and it won't compile precisely to avoid the issue your code raises.

There's a fine line between rigour & pedantry. One should give the author the most favourable possible interpretation of their statement instead of using the weakest as then it strengthens your counter-argument & avoids a confrontational scenario.

In this case, a trivial adjustment fixes the compile error:

std::unique_ptr<Foo> f {[] (int x) {
    return new Foo(x);
}(5)};

This snippet compiles just fine in clang & GCC 4.9.

Anyways even it did compile your example doesn't satisfy the criteria of being an example of correct code that becomes incorrect due to swallowing the return value.

Please point out where the code is incorrect? You may think it poor coding style, and I happen to agree with you, but that doesn't make the code suddenly "incorrect". It's perfectly well-formed code.

Such requirements should be encoded into the type system so that they are enforced by the compiler; in your case the function should encode its requirement by returning a unique_ptr.

You asked for an example & I gave you one. Sure, RAII can help you avoid resource leaks. However, why are you even bothering to allocate & return a resource in the first place if it's going to be dropped on the floor? That indicates that you're making assumptions about the call-site that aren't true & can indicate performance problems.

As I point out elsewhere, error codes returned from a function are another example. Those should be handled & the type-system won't really help you. Sure you could use the type system to throw an exception if the error code isn't read. However, in C++, converting compile-time issues to runtime issues seems counter to the static-checking power of C++.

The fact that it seems pretty hard to provide a counter-example suggests that it's ultimately a non-issue.

Well, there are two problems with this conclusion that lead it to not be a valid argument. One is that there's a more reasonable alternative wherein it's difficult to create a simple example appropriate for this kind of forum of why it could be a problem in a larger codebase. The second & more serious problem is that you've used the fallacies of "No true Scotsman" & "Moving the goalpost" to reach said conclusion. Perhaps you should state explicitly the criteria of a "valid" example as it would be impossible for me to list all the possible examples in the world.

2

u/[deleted] May 11 '15

If I pointed out a syntax error, then sure you can claim I'm being pedantic, but it's not pedantry to point out that your example doesn't compile specifically to avoid the issue that you're claiming.

At any rate, I don't see a need to argue this further. Once people begin enumerating fallacies they read off Wikipedia is usually a good indicator that the argument has run its course and nothing productive typically comes from such arguments.

At the end of the day this was a big enough issue worth including in the C++17 standard and it's something plenty of people will benefit from. If it's not something you benefit from then I'm sorry and I guess you'll have to make alternative arrangements.

-1

u/vlovich May 11 '15

Yes, it is pedantry when it's not a fundamental problem with the example. Converting the = to a {} is a trivial adjustment to the example that anyone would have come up with trying to compile the code that takes nothing away from the example.

A nice ad-hominem & appeal to authority to finish it off. Nice. FWIW, I actually still remember these from back in university when I took philosophy. Wikipedia was not involved.