<Insert Picture Here> GCC and C++, again

Similar documents
Fixing Atomic Initialization, Rev1

C++11/14 Rocks. Clang Edition. Alex Korban

CS93SI Handout 04 Spring 2006 Apr Review Answers

DDMD AND AUTOMATED CONVERSION FROM C++ TO D

Qualified std::function signatures

Rvalue References & Move Semantics

Part X. Advanced C ++

A brief introduction to C++

Lecture 10: building large projects, beginning C++, C++ and structs

This is a talk given by me to the Northwest C++ Users Group on May 19, 2010.

Chapter 17 vector and Free Store. Bjarne Stroustrup

C++ Module TS Issues List Gabriel Dos Reis Microsoft

Slide Set 6. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Chapter 1 Getting Started

C++ Coding Standards. 101 Rules, Guidelines, and Best Practices. Herb Sutter Andrei Alexandrescu. Boston. 'Y.'YAddison-Wesley

Advanced C++ Programming Workshop (With C++11, C++14, C++17) & Design Patterns

Vector and Free Store (Pointers and Memory Allocation)

Tutorial 7. Y. Bernat. Object Oriented Programming 2, Spring Y. Bernat Tutorial 7

Modules:Dependent ADL

Smart Pointers, deleted functions, and 2-3 trees

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Resource Management With a Unique Pointer. Resource Management. Implementing a Unique Pointer Class. Copying and Moving Unique Pointers

QStringView. everywhere. Marc Mutz, Senior Software Engineer at KDAB

Object-Oriented Principles and Practice / C++

And Even More and More C++ Fundamentals of Computer Science

Rvalue References, Move Semantics, Universal References

Object-Oriented Principles and Practice / C++

Chris' Makefile Tutorial

Overload Resolution. Ansel Sermersheim & Barbara Geller Amsterdam C++ Group March 2019

Proposed Wording for Concurrent Data Structures: Hazard Pointer and Read Copy Update (RCU)

CSE 303: Concepts and Tools for Software Development

Financial computing with C++

C++11 and Compiler Update

AIMS Embedded Systems Programming MT 2017

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

Increases Program Structure which results in greater reliability. Polymorphism

CS/ENGRD 2110 SPRING Lecture 3: Fields, getters and setters, constructors, testing

CSE 333. Lecture 11 - constructor insanity. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

Modernizing legacy C++ code

Synchronization. CS61, Lecture 18. Prof. Stephen Chong November 3, 2011

Functions, Pointers, and the Basics of C++ Classes

Overload Resolution. Ansel Sermersheim & Barbara Geller ACCU / C++ June 2018

CS11 Advanced C++ Spring 2018 Lecture 2

Vector and Free Store (Vectors and Arrays)

Short Notes of CS201

Term Paper. Daniel Sarnow. Interface-based Programming in C++ Fakultät Technik und Informatik Studiendepartment Informatik

Lab 4: Tracery Recursion in C with Linked Lists

Tutorial 6. Y. Bernat. Object Oriented Programming 2, Spring Y. Bernat Tutorial 6

G52CPP C++ Programming Lecture 20

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

CS201 - Introduction to Programming Glossary By

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

Supporting Class / C++ Lecture Notes

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming


September 10,

Here's how you declare a function that returns a pointer to a character:

A Generic Non-intrusive Smart Pointer Implementation

Object-Oriented Programming for Scientific Computing

CS3157: Advanced Programming. Outline

COMP 2355 Introduction to Systems Programming

QUIZ. How could we disable the automatic creation of copyconstructors

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Control Abstraction. Hwansoo Han

An Insight Into Inheritance, Object Oriented Programming, Run-Time Type Information, and Exceptions PV264 Advanced Programming in C++

Part III. Advanced C ++

Chapter 17 vector and Free Store

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures

Simulating Partial Specialization

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

QUIZ. How could we disable the automatic creation of copyconstructors

Advanced Systems Programming

Chapter 18 Vectors and Arrays [and more on pointers (nmm) ] Bjarne Stroustrup

Sample Copy. Not for Distribution.

Object-Oriented Principles and Practice / C++

Linked Lists. What is a Linked List?

Rethinking the core OS in 2015

C The new standard

Learning to Program with Haiku

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Assignment operator string class c++ Assignment operator string class c++.zip

Modern and Lucid C++ Advanced for Professional Programmers. Part 3 Move Semantics. Department I - C Plus Plus Advanced

Absolute C++ Walter Savitch

Exercise Session 2 Systems Programming and Computer Architecture

D Programming Language

Axivion Bauhaus Suite Technical Factsheet AUTOSAR

Why Deprecating async() is the Worst of all Options

COSC 2P95 Lab 5 Object Orientation

All code must follow best practices. Part (but not all) of this is adhering to the following guidelines:

However, in C we can group related variables together into something called a struct.

Chapter 10 :: Data Abstraction and Object Orientation

Lecture Notes on Memory Layout

OCaml Language Choices CMSC 330: Organization of Programming Languages

Why use inheritance? The most important slide of the lecture. Programming in C++ Reasons for Inheritance (revision) Inheritance in C++

Object Oriented Software Design II

III. Classes (Chap. 3)

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

C++ for System Developers with Design Pattern

Provided by - Microsoft Placement Paper Technical 2012

Transcription:

<Insert Picture Here> GCC and C++, again Paolo Carlini PhD

Outline 23 years of GCC! http://www.youtube.com/watch?v=zealhvoz8qq Since the last Summit, GCC 4.7.0 is out and C++11 is also out! A lot happened over the last year to implement more C++11 features And shake bugs affecting the existing ones Non-trivial projects are already globally enabling -std=c++11 (or -std=c++0x) in the builds.

Outline (2) Important GCC 4.7 features outside the C++ front-end are not discussed in the following, eg: General optimizer improvements (eg, LTO much improved for large programs, like Firefox; string length optimization) Back-end changes (AVX2 support for x86, Sparc improvements) Improvements for other OSes besides Linux (eg, <thread> is now supported for most Posix targets) OpenMP 3.1

Outline (3) Diagnostic improvements New C++11 features Torvald Riegel will cover the C++11 memory model, TM, new features in the concurrency area

-Wunused-local-typedefs Resolving libstdc++/33084 boiled down to fixing a library (ie, <valarrray>) function with this body: typedef _BinClos<_Name, _Constant, _ValArray, _Tp, _Tp> _Closure; typedef typename fun<_name, _Tp>::result_type _Rt; return _Expr<_Closure, _Tp>(_Closure( t, v)); Note the pointless typedef...

-Wunused-local-typedefs (2) indeed we had a trivial typo: _Rt _Tp In PR33255 I wondered if we could do something about this! In 4.7, the new -Wunused-local-typedefs warning (implemented by Dodji Seketeli) detects such sort of very suspect unused typedef. In 4.8, will be enabled by default as part of -Wunused have to make sure we don't give spurious warnings in some special cases unrelated to the typedef case proper

-Wzero-as-null-pointer-constant In C++11 there is a proper type for null pointer constants, std::nullptr_t, with value nullptr (*), eg: int* p = nullptr; preferably replaces: int* p = 0; Likewise in conditionals, everywhere. (*) http://en.wikipedia.org/wiki/c++11#null_pointer_constant

-Wzero-as-null-pointer-constant (2) The new -Wzero-as-null-pointer-constant, available in c++98 mode too, detects such uses of the legacy 0 literal to mean null pointer and helps moving code to C++11. First blush, it seems a trivial thing I did the work mostly to answer a PR and while doing that learning more about the C++ front-end but apparently quite a few users are finding it useful... because we got many PRs when the features was still buggy, and one for 4.7.0 too! The latter fixed for 4.7.1, was about 0 in default arguments.

-ftrack-macro-expansion Consider this simple example (provided by Dodji in the audit of PR7263) involving a buggy macro: #define OPERATE(OPRD1, OPRT, OPRD2) OPRD1 OPRT OPRD2 #define SHIFTL(A,B) OPERATE (A,<<,B) #define MULT2(A) SHIFTL (A,1) void g () { MULT2 (1.0); /* 1.0 << 1; */ }

-ftrack-macro-expansion (2) In 4.6, or without -ftrack-macro-expansion, the user only gets an error message about line #8, where the macro is actually expanded, has no clue that in fact the problem happens in the OPERATE macro, much earlier in the code. Isn't the default behavior (just yet) because a little more memory is used. Help testing it! This is useful for C too, of course. And this is also catching up with Clang++ ;) Likewise for -Wdelete-non-virtual-destructor Likewise for PR48934, about errors with SFINAE overloads

New C++11 features For a general summary, always refer to: http://gcc.gnu.org/gcc-4.7/cxx0x_status.html besides the release notes: http://gcc.gnu.org/gcc-4.7/changes.html

New C++11 features (2) Here only a couple of examples In my blog: https://blogs.oracle.com/pcarlini/ I'm posting concise introductions to many C++11 features, those delivered in GCC 4.7 included, for instance: Template aliases User-defined literals Non-static data-member initializers Explicit overrides and final

First example: explicit overrides and final In C++11 the identifiers final and override acquire a special meaning in some contexts: struct B1 final { }; struct D1 : B1 { }; // "cannot derive from 'final' base" struct B2 { virtual void f() final {} // "overriding final function" }; struct D2 : B2 { virtual void f() {} // "error" };

Explicit overrides and final (2) In both cases the code is rejected. Dually, this is also rejected: struct B3 { virtual void g(int) {} }; struct D3 : B3 { virtual void g(double) override {} // "does not // override" };

Explicit overrides and final (3) Thus a whole class of nasty bugs is easily avoided, like, in the case of D3::g, a new function inadvertently declared, instead of overriding an existing one. But, interestingly, this isn't just about avoiding bugs! In GCC 4.7, thanks to a nifty patch provided by Roberto Vitillo, calls of a virtual function marked final, like B2::f above, are devirtualized, thus optimized to more efficient assembly. Likewise for any virtual function declared in a class decorated with final as a whole, like B1 above.

Explicit overrides and final (4) Unfortunately, in order to deal correctly with final classes like B1 above, the entire C++ library has to be adjusted to carefully avoid in such cases the widespread Empty Base Optimization. A lot of work went into GCC 4.7 to handle correctly all these situations. But it seems that something better could be done in terms of C++11 specs... naively, it doesn't really make sense to renounce to the space saving advantages provided by EBO simply because a class is final!

noexcept and the library (2) The testcase (simplified): struct Stuff { Stuff( ) { } Stuff(Stuff&&) /* noexcept */ { /*... */ } Stuff(const Stuff&) { /*... */ } }; int main() { std::vector< Stuff > stuff; stuff.push_back( Stuff() ); stuff.push_back( Stuff() ); /*... */ }

noexcept and the library (3) In C++11, as released: no noexcept on the moveconstructor no safe vs exceptions move-semantics no move-semantics at all! Summarizing: If you are writing your own move-constructor, do your best to have it not throwing exceptions and, as such, explicitly decorate it with noexcept. Much more generally: noexcept is the future, old-style throw() exception specifications are deprecated in C++11, and 4.8 will likely warn for those.

Conclusions (for Paolo's talk) Please also send your ideas, observations, etc, to: gcc@gcc.gnu.org libstdc++@gcc.gnu.org... or simply to me ;) paolo.carlini@oracle.com

Thanks!

Random bibliography Some recent C++11 books: http://www.manning.com/williams/ http://www.cppstdlib.com/