Monday, March 3, 2008

Q&A with Herb Sutter and Bjarne Stroustrup

Grill the experts:

On tips for using TR1 (the first technical report of library extensions):
  • boost implements TR1 now, MS will soon
  • things are in std::tr1 now, use this
  • don't overuse smart_ptr<>

Bjarne has a new book coming out for teach programming principles and practices with C++. Toward the end he uses some of the TR1 stuff.

Java has good memory management, but still not the resource management C++ has because of constructors/destructors. RAII.

C++0x hopes to have more features for novices.

Comments on C++98's exceptions specification - don't use them.

  • i.e. Constructor::Constructor() throw(Exceptions)

Bjarne Stroustrup and me



How to design good interfaces

Bjarne Stroustrup

KISS principle

Don’t use f(pointer, count)

Interface principles
1. Simple
2. Stable
3. Evolvable (extensible)
4. Explicit
5. Catch errors
6. Consistent
7. Efficient

Simple/concrete then abstract/generalize. If in doubt, don’t put it in.

Not all interfaces are the same, nor should they be.

Design parameters (checklist)
1. Type safety
2. Mutability
3. Customization
4. Error handling
5. Performance
6. Concurrency
7. Resource management
8. Lifetime
9. Copying

(See slides on developing a project’s design parameters)

Aims of design
· Represent ideas directly in code
o Data structures
o Algorithms
· Represent ideas independently in code
· Represent relationships among ideas directly in code
o Hierarchy
o Parameterization
· Combine ideas freely

Class vs. Plain Old Data (struct) design

Use struct if you can’t define an invariant

Define an invariant for ever class (that’s not a struct) and provide ways of checking and enforcing invariants.

Be explicit about mutability! (const correctness)

Classic Bjarne rule: RIIA “resource acquisition is initialization”
· Keys to exception safety and performance
· Capture resources (i.e. memory, file, socket, thread handle) with constructors
· Release resources with destructors

C++0x: An overview

Bjarne Stroustrup

The basics rarely change.
Sat, Mar 1: C++ standards meeting, stuff voted in (lambda functions).
C++ supports OO, generic programming, etc.
The best solutions often cross these boundaries.

C++ Ox aims
· better language for systems programming and library building
· being easier to teach/learn.

Portability = good efficiency at the bottom and good abstraction at the top.
In C++Ox the x may be 9, or hexadecimal.
Google ‘WG21’ for details or to track standards committee.
Essentially all new standard libraries are available now: boost.org, M$, etc
Features are starting to appear in compilers (suggestions: experiment with them now, don’t put in production code yet).

C++ Ox ideals
· Maintain stability and compatibility
· Prefer libraries to language extensions
· Prefer generality to specialization
· Fit into existing toolchains

Language summary: ~35 new features!
Libraries summary: ~11 new libraries – RE, Threads, Networking, etc (all are now available)

Area of language change
· Machine model and concurrency (writings will come out within 2 weeks – search ‘WG21’
o Thread library
o Thread local storage
o Atomic ABI
o Async message bufer
· More support for generic programming
· Improved enums, long long, C99 character types, static_assert
· Initializations
· Postponed:
o Modules and dynamic linked libraries
o Programmer controlled garbage collection

Initalizer lists

template class vector {
// ...
vector(std::initializer_list); // sequence constructor
// ...
};

vector v = {1, 2, 3, 4};
vector heroes = {“Bjarne”, “Booch”};

Also:

void f(int, std:: initializer_list, int);
f(1, {1,2,3,4}, 5);
f(1, {1,a,x+y}, 0);

Lambda functions

Example: [](int x) { return x<7; }

Also:

[&x] grab x by reference
[x] copy x
[&] grab everything by reference

“C++0x will be a better tool than C++98 – much better.”

I'm an official geek

I'm an official geek

Concert or conference?

Concert or conference?

txt pic check

txt pic check

txt entry check

txt entry check