Preconditions, Part 1 -- Andrzej KrzemieĊ„ski

On preconditions, and their compile time enforcement with static_assert and a dash of regex.

Preconditions, Part 1

by Andrzej Krzemieński

In this post, I want to share my thoughts about the notion of precondition. In “Design by Contract” philosophy, preconditions are always mentioned along postconditions and invariants, and in the context of OO design. In this post I focus only on preconditions and not necessarily related to any class. For instance, the following function specifies a precondition on its argument:

double sqrt(double x);

// precondition: x >= 0

Note that the function specifies the precondition even though there is no language feature for this purpose (at least in C++). A precondition is a “concept” or an “idea” rather than a language feature. This is the kind of preconditions that this post is about.

Continue reading...

C++ and Beyond 2012: Panel - Convincing your Colleagues

A new C++ and Beyond 2012 panel is now available:

C++ and Beyond 2012: Panel - Convincing your Colleagues

From C++ and Beyond 2012, Andrei, Herb and Scott present Convincing Your Colleagues - an interactive panel.

Abstract:

You can't do a better job if you don't change what you're doing, but change is hard.  It's especially hard when what needs to change is your colleagues' approach to software development. Moving your team forward often requires persuading your peers to change their behavior, sometimes to do something they're not doing, other times to stop doing something they've become accustomed to.  Whether the issue is to embrace or avoid C++ language features, to adopt new development tools or abandon old ones, to increase use of or scale back on overuse of design patterns, to adhere to coding standards, or any of the plethora of other matters that affect software creation, moving things forward typically requires getting your colleagues to buy into the change you're proposing.  But how can you do that?

In this panel session, Andrei, Herb, and Scott share how they go about convincing their colleagues to change and take questions from the audience.

 

C++ and Beyond 2013 dates announced

Scott Meyers has announced dates for another C++ and Beyond seminar with Scott, Herb Sutter, and Andrei Alexandrescu:

There will be a C++ and Beyond 2013!  The dates are tentatively set to be December 8-11, so block that time out on your calendar.  We have a venue picked out, but we haven’t signed a contract yet, so I can’t make any announcement about where C&B 2013 will take place.  I expect to be able to tell you all about it within the next few weeks, however.

Until then, hold December 8-11, 2013, for more in-depth, in-sightful, in-triguing, in-vestigations into the world of C++... and Beyond grin

Scott

SG 9 (Ranges) mailing list is now open

Marshall Clow, chair of SG9 (Ranges) has announced the SG9 mailing list is now open:

Sign up at http://www.open-std.org/mailman/listinfo/ranges.

I'll post a welcome message to start off the discussion on Wednesday morning; please hold off on posting until then so that people have a chance to join.

Thanks - and have a Happy New Year!

-- Marshall

P.S. thanks to Keld [Simonsen] for setting up the list.

You Don't Know const and mutable -- Herb Sutter

In August, Herb Sutter gave a brand new 30-min talk at C++ and Beyond where he laid out the fundamentally new meanings of two long-time C++ keywords:

You Don't Know [const] and [mutable]

Herb Sutter

There’s a major change in C++11 that [...] rewrites pre-C++11 design guidance and is directly related to writing solid code in a concurrent and parallel world. And it isn’t just academic — everyone is going to have to learn and apply the new C++11 guidance that we’ll cover in this session.

This is a great example of how C++11 is a simpler language: We can stop the Cold War-era waffling about subtleties about what 20th-century C++ const means, and proudly declare modern C++ const has the simple and natural and "obvious" meaning that most people expected all along anyway.

One of the most common questions after the talk was, "Do other C++ experts agree with Herb's conclusions?" The answer is yes -- regarding his upcoming The C++ Programming Language, Fourth Edition, Bjarne Stroustrup writes: "I do point out that const means immutable and absence of race conditions in the last Tour chapter. I plan more for the concurrency chapter." Look for the third Tour chapter to be posted here next week, and the aforementioned last Tour chapter to be posted here in early February.

Enjoy.

Using constexpr to Improve Security, Performance and Encapsulation in C++ -- Danny Kalev

How is constexpr different from const, and how does it contribute to making modern C++ code cleaner and simpler, as well as faster than ever? Danny Kalev gives a nice summary:

Using constexpr to Improve Security, Performance and Encapsulation in C++

by Danny Kalev

constexpr is a new C++11 keyword that rids you of the need to create macros and hardcoded literals. It also guarantees, under certain conditions, that objects undergo static initialization. Danny Kalev shows how to embed constexpr in C++ applications to define constant expressions that might not be so constant otherwise.

Continue reading...

Quick Q: A unique_ptr is not copyable, so why can I return one by value? -- StackOverflow

 

A common question for programmers new to C++11 and its new features: "Hey, look how easy this was! ... But, um, why and how did it work?"

Returning unique_ptr from functions

unique_ptr<T> does not allow copy construction, instead it supports move semantics. Yet, I can return a unique_ptr<T> from a function and assign the returned value to a variable.

#include <iostream>
#include <memory>
using namespace std;

unique_ptr<int> foo()
{
  unique_ptr<int> p( new int(10) );
  return p;                   // 1
  //return move( p );         // 2
}

int main()
{
  unique_ptr<int> p = foo();
  cout << *p << endl;
  return 0;
}

The code above compiles and works as intended. So how is it that line 1 doesn't invoke the copy constructor and result in compiler errors? If I had to use line 2 instead it'd make sense (using line 2 works as well, but we're not required to do so).

I know C++0x allows this exception to unique_ptr since the return value is a temporary object that will be destroyed as soon as the function exits, thus guaranteeing the uniqueness of the returned pointer. I'm curious about how this is implemented, is it special cased in the compiler or is there some other clause in the language specification that this exploits?

Continue reading...

Qt 5 C++ UI framework released

Qt 5 is now available. This is the first major release under Digia, who recently acquired Qt from Nokia. Key additions include support for C++11 and HTML5 along with an integrated WebKit2 browser engine. Both commercial and open source versions are available.

Qt is the most widely-known and -used portable UI framework for C++, used by nearly half a millon developers worldwide. It offers native code performance and modern sophisticated user experiences across desktop, embedded, and mobile platforms.

More details from the Qt 5 product page:

Our 5th big iteration deepens four essential aspects of the Qt offering:

Amazing graphic capabilities and performance, especially manifested in constrained environments like embedded and mobile devices. Qt Quick 2 offers a GL-based scene graph, a particle system and a collection of shader effects. Qt Multimedia and Qt Graphical Effects bring these features even further.

Developer productivity and flexibility, making JavaScript and QML first class citizens while keeping the C++ foundation and Qt Widget support. The addition of Qt WebKit 2 should make HTML5 developers feel at home.

Cross-platform portability is now simpler for OS developers thanks to the new structure of Essentials and Add-ons modules, plus the consolidation of Qt Platform Abstraction. We look forward to seeing Qt running in all kinds of environments! Next up is full Qt support on iOS and Android and work here has already begun.

Open development and open governance is assuring wider development and testing of Qt 5 by a growing community including developers from Digia, KDAB, Intel, Collabora, Accenture, the KDE project, and many more companies and individuals. Today we all celebrate!

What’s new:

 

  • Amazing Graphics Capability and Performance
  • Qt Quick in Qt 5
  • WebKit and HTML5
  • Multimedia
  • Modularized Qt Libraries
  • Widgets in Qt 5
  • Qt Platform Abstraction
  • New Connection Syntax
  • Connectivity and Networking
  • JSON Support
  • User Input

 

Continue reading: