SlideShare a Scribd company logo
1 of 42
C++ Eleventy for ATS
Style and Suggestions
Introduction
 C++11 (“eleventy”) is a major change in the language (Lisp rulz!)
 7.0 release requires compilers that are C++11 compliant.
 Take advantage of new language features without going crazy.
 You know who you are.
 Focus here is on ATS related features, not an overview of all changes.
 Looking at just the features I think are the most useful for ATS coding.
Apache Traffic Server Summit Fall 2016 2
nullptr
 A null pointer.
 Never worse and frequently better than
 NULL
 0
 nullptr is of type nullptr_t - overloads are distinct from ‘0’ or NULL.
 Classes that involve pointers and numbers (e.g., strings) can now differentiate ‘0’
as “zero” vs. “null”.
 This is required.
 Do not use NULL.
 Should not use ‘0’ either to mean NULL.
Apache Traffic Server Summit Fall 2016 3
auto
Make the compiler figure out the type
 Useful to avoid having to write a type twice.
 auto thing = new Thing;
 Types it would take days to figure out and don’t really matter.
 Iterators, complex containers, lambdas.
 Local uses where the type is unimportant
 locally scoped variables.
 {
auto tmp = lhs;
lhs = rhs;
rhs = tmp;
}
 for loop variables (but see later section)
Apache Traffic Server Summit Fall 2016 4
auto
 Qualifiers can be added to the computed type.
 const, volatile, lvalue reference, rvalue reference, pointer.
 Requires an initialization expression from which to deduce the type.
Apache Traffic Server Summit Fall 2016 5
auto
Don’t drive others crazy
 Easy to overuse.
 Don’t make people guess about the type.
 Should be obvious or irrelevant.
 Be extra careful with numerics.
 The compiler doesn’t always think it’s the same type you do.
 In many cases in ATS the numeric type needs to be very specific.
Apache Traffic Server Summit Fall 2016 6
Range-based for loops
 Iterate over elements in a container.
for (auto& item : acl_list) {
item.update();
// …
}
 Container must support C++ standard iteration via
 begin / end methods
 begin / end functions
 An array with a declared length.
 The variable for elements can be a value which will be a copy of the actual
element.
 The variable does not have to be declared auto but that’s easier.
Apache Traffic Server Summit Fall 2016 7
Methodoloy
Useful advancements in method technology
Apache Traffic Server Summit Fall 2016 8
Delegating Constructors
 init() methods are error prone and annoying.
 C++11 makes it possible to call a constructor from another constructor using
member initialization syntax.
class Leif {
int a;
public:
Leif(int x) { if (0<x && x<=max) a=x; else throw bad_Leif(x); }
Leif() : Leif{42} { }
Leif(string s) : Leif{lexical_cast<int>(s)} { }
// ...
};
Apache Traffic Server Summit Fall 2016 9
Prohibiting methods
 Methods can be explicitly removed from the class.
class X {
X& operator=(const X&) = delete; // Disallow copying
X(const X&) = delete;
};
 Use this instead of declaring but not defining these methods.
 This works for any method including inherited methods.
Apache Traffic Server Summit Fall 2016 10
Defaulting methods
 Methods can be defined as “default” which means use the compiler generated
method.
 Can only be used for methods that have compiler definitions.
 Theoretically not useful because doing nothing has the same effect.
 Always use instead of defining a method that replicates default behavior.
 Make it explicit the default is being used.
Apache Traffic Server Summit Fall 2016 11
Member Initialization
 Class members can be initialized directly in the class declaration.
 Overridden by constructor
 Avoid unobvious repetition in multiple constructors
Class Leif {
Leif() : left(2), right(3), hair(HAT), phrase(“kill it with fire”),
yet(4), another(5), member(7) {}
Leif(const char *text) : left(2), right(3), hair(HAT), phrase(text),
yet(4), another(5), member(7) {}
};
// vs.
class Leif {
Leif() {}
Leif(char const* text) : phrase(text) {}
int left = 2;
int right = 3;
HairStyle hair(HAT);
// …
};
Apache Traffic Server Summit Fall 2016 12
EThread::EThread() : generator((uint64_t)ink_get_hrtime_internal() ^ (uint64_t)(uintptr_t) this),
ethreads_to_be_signalled(NULL),
n_ethreads_to_be_signalled(0), main_accept_index(-1), id(NO_ETHREAD_ID), event_types(0), tt(REGULAR),
current_metric(metrics + (ink_get_hrtime_internal() / HRTIME_SECOND) % N_EVENT_METRICS)
{
memset(thread_private, 0, PER_THREAD_DATA);
}
EThread::EThread(ThreadType att, int anid)
: generator((uint64_t)ink_get_hrtime_internal() ^ (uint64_t)(uintptr_t) this), ethreads_to_be_signalled(NULL),
n_ethreads_to_be_signalled(0), main_accept_index(-1), id(anid), event_types(0), tt(att),
server_session_pool(NULL), current_metric(metrics + (ink_get_hrtime_internal() / HRTIME_SECOND) % N_EVENT_METRICS)
{
}
EThread::EThread(ThreadType att, Event *e)
: generator((uint32_t)((uintptr_t)time(NULL) ^ (uintptr_t) this)), ethreads_to_be_signalled(NULL),
n_ethreads_to_be_signalled(0),
main_accept_index(-1), id(NO_ETHREAD_ID), event_types(0), tt(att), oneevent(e), current_metric(metrics +
(ink_get_hrtime_internal() / HRTIME_SECOND) % N_EVENT_METRICS)
{
ink_assert(att == DEDICATED);
memset(thread_private, 0, PER_THREAD_DATA);
} Apache Traffic Server Summit Fall 2016 13
class EThread : public Thread
{
// ... The initialized members
InkRand generator = static_cast<uint64_t>(Thread::get_hrtime_updated() ^
reinterpret_cast<uintptr_t>(this));
EThread **ethreads_to_be_signalled = nullptr;
int n_ethreads_to_be_signalled = 0;
unsigned int event_types = 0;
Event *start_event = nullptr;
ServerSessionPool *server_session_pool = nullptr;
};
EThread::EThread() : id(NO_ETHREAD_ID), tt(REGULAR)
{
memset(thread_private, 0, P ER_THREAD_DATA);
}
EThread::EThread(ThreadType att, int anid) : id(anid), tt(att), server_session_pool(nullptr)
{
// …
}
EThread::EThread(ThreadType att, Event *e) : id(NO_ETHREAD_ID), tt(att), start_event(e)
{
memset(thread_private, 0, PER_THREAD_DATA);
}
Apache Traffic Server Summit Fall 2016 14
override
 Marks a method as overriding a super class method.
 No code effect.
 Useful to be sure the method really does override.
 Recommended.
Apache Traffic Server Summit Fall 2016 15
Explicit conversion operators
 User type conversions can be marked explicit.
 This allows direct conversions but not implicit conversions.
 Classic example – conversion to bool.
 In C++98 this also allows further conversion to pointer or numeric types.
 Worked around by making the conversion to a pointer to method.
 In C++11 mark conversion explicit.
Apache Traffic Server Summit Fall 2016 16
More stuff
Apache Traffic Server Summit Fall 2016 17
std::unique_ptr<T>
#include <memory>
 Dynamically allocated object that is not shared.
 Default constructed to contain nullptr.
 When the instance is destroyed or has another value assigned it is cleaned up.
 Clean up mechanism can be changed, defaults to the destructor for T.
 ats_scoped_XXX is modeled on this class.
 Change should be easy for this reason.
 Always intended as a temporary measure until we could use C++ eleventy.
Apache Traffic Server Summit Fall 2016 18
std::unique_ptr<T>
 Useful for
 Class members that cannot be statically declared but share the lifetime of the
class.
 Local temporary heap storage.
Apache Traffic Server Summit Fall 2016 19
Lambda
 Lambda is a notation that creates an anonymous functor class and instantiates
it.
 [] () mutable throw() -> T { /* … */ }
capture parameters optional optional return type code
 Common functionality in ECMA style languages (Perl, Python, JavaScript, …)
 Lambdas can be used as temporaries or stored
 Via auto or std::function.
 It’s C++, both the lambda and its container are strongly typed.
 Lambdas can bind objects in current scope either by value or reference.
 Be careful of variable lifetime vs. lambda lifetime.
 Value binding vs. mutable – by default values are const.
Apache Traffic Server Summit Fall 2016 20
Lambda Capture
 Capture syntax basic
 “[=]” -> capture by value.
 “[&]” -> capture by reference.
 These capture any name used in that lambda that is not in the lambda scope but in
the enclosing scope.
 Capture syntax by name
 “[=name]” -> capture “name” by value.
 “[&, =thing1, =thing2]” -> capture all names in use by reference except “thing1”
and “thing2” which are captured by value.
Apache Traffic Server Summit Fall 2016 21
Lambda curry
 Lambdas can be used to curry and bind functions.
 Create shims between the function you have and what the API needs.
 Change the set of parameters or fix parameters to specific values.
 Create a function object from an object instance and a method.
 Make Standard Template Library algorithms much more useful.
Apache Traffic Server Summit Fall 2016 22
Example
typedef std::function<void (const char*)> CallBack;
void SetCallback(CallBack cb);
int some_function(const unsigned char* input) { … }
// … some other code site, convert some_function to expected callback form
SetCallBack(lambda [] (const char* s) -> void {
(void) some_function(reinterpret_cast<const unsigned char*>(s);
}
Apache Traffic Server Summit Fall 2016 23
std::function
#include <functional>
 Holds an object that can be called as a function.
 Function, functor, lambda.
 Defined with a specific signature.
 Simplifies providing flexibility to caller.
 Good for global or process start callbacks.
 Can be used with lambdas to curry functions or pass method callbacks.
 NOTE: This stores the function pointer only. If bound to a method it does not
preserve the object.
 May require allocation if passed a functor or lambda.
Apache Traffic Server Summit Fall 2016 24
constexpr
 Marks a variable, method, or function as compile time computable.
 For variables, used in place of const.
 Static methods work as functions.
 Dynamic methods work if class instance used is constexpr.
 Opposite of taint, “pure”.
 If all inputs are constexpr then result is constexpr.
 If any input is tainted (not constexpr) neither is result.
 Can be used in non-compile time constant context.
 Purpose – do more general and complex compile time computations.
Apache Traffic Server Summit Fall 2016 25
constexpr
 Consider a simple computation like
# define BUFFER_SIZE_FOR_INDEX(i) (DEFAULT_BUFFER_BASE_SIZE * (1 << (i)))
 This is reduced to a constant during compilation. If an inline function were
used
inline size_t BUFFER_SIZE_FOR_INDEX(int i) {
return DEFAULT_BUFFER_BASE_SIZE * (1<< i));
}
 This would be cleaner but no longer always a constant. constexpr makes it
possible to have the best of defines and inline functions.
inline constexpr size_t BUFFER_SIZE_FOR_INDEX(int i) { … }
Apache Traffic Server Summit Fall 2016 26
static_assert
 Compile time assertion.
 Two arguments, a constexpr and a string.
 If the expression is not true, the compilation fails and the string is printed.
 Equivalent used many places in ATS, either done dynamically or via singleton
constructor.
 Can use type information unlike #if.
static_assert(INK_START_ERRNO > SOCK_ERROR, “Internal errono values overlap
system errno values”);
Apache Traffic Server Summit Fall 2016 27
Rvalue reference
 New qualifier – rvalue reference. “int&& rvr = 4;”
 lvalue vs. rvalue
 lvalue has a name / specified by programmer.
 rvalue has no name / created by compiler (“temporary”).
 “Universal reference”
 “&&” can mean either lvalue or rvalue reference if the type it qualifies is deduced
by compiler.
 Syntactic sugar only – all actual instantiations are lvalue or rvalue reference.
Apache Traffic Server Summit Fall 2016 28
Perfect Forwarding
 Addition of rvalue and “universal” references allows a function F to forward
its arguments exactly to another function.
 Root cause – temporaries vs. variables.
 Previously this took 2N template implementations for an N parameter function.
 Technically possible but rarely, if ever, done in practice.
 The key trick is a “universal reference” can evolve in to an lvalue or rvalue
reference as needed to forward the argument.
 This is an obscure technique – if it’s not clear, don’t try to use it.
Apache Traffic Server Summit Fall 2016 29
Move
 “Move semantics” is a combination of
 Bitwise copy
 Destructor skipping
 Object A is bitwise copied to object B without the destructor or constructor of A or
B invoked.
 Useful in specific situations where the contents of A are important but
neither A itself nor the contents of B are.
 But those situations show up very frequently.
 B is a default constructed container element.
 B is a return value.
 B is raw memory that has never been constructed.
Apache Traffic Server Summit Fall 2016 30
std::move
 Template formalization to provide additional control of movement.
 Specialize if needed for specific types.
 When used it should be expected to result in the destruction of the object.
 In many ways like auto_ptr but not done stupid.
// Fast container add that avoids a copy
Thing q;
Container c;
c.emplace(std::move(q));
// assume q is invalid at this point.
Apache Traffic Server Summit Fall 2016 31
Enum class, storage
 Enumerations can be classes.
 Creates type difference from numeric types.
 Constrains name space injection.
 Good for global / file scope enums.
 Class nested enums should be plain.
 Enums can specific storage class.
 Optimize memory footprint.
 Allow forward declaration / in name only.
Apache Traffic Server Summit Fall 2016 32
Uniform initialization
 Bit of a misnomer but allows a wide variety of initializations with the same
syntax.
 Aggregate initialization.
 Constructor invocation.
 Uniform initialization can never be mistaken for a function definition.
 POD without a constructor is considered an aggregate.
 For PODs with few members very handy. Pass temporary without writing a trivial
constructor.
 Notation is recursive.
Apache Traffic Server Summit Fall 2016 33
Uniform initialization
 Can be overridden with constructor.
 Single argument of type std::initializer_list<T>.
 Note the list must be homogenous.
 Can be used in container loops.
for ( auto item const& : { “one”, “two”, “three” } ) { … }
 This can avoid having to declare an array used in only one loop.
Apache Traffic Server Summit Fall 2016 34
Uniform initialization examples
struct Leif {
int q;
const char* phrase;
};
void f()
{
Leif l = Leif{1, “ack”};
Leif l{1, “ack”};
Leif clones[4] = { {1, “ack”}, {2, “make it so”},{3, “pebkac”},{4, “use lua!”}};
}
Apache Traffic Server Summit Fall 2016 35
Uniform Initialization – another example
struct Exile { bool deposed_p, const char* name, const char* reason; };
void make_soylent_green(Exile&& victim);
make_soylent_green(Exile { true, “Leif”, “too much hat wearing” });
// or even
make_soylent_green({ true, “Leif”, “too much hat wearing”});
Apache Traffic Server Summit Fall 2016 36
Raw Strings
 Raw string literals allow alternate string delimiters to simplify escaping.
auto str = R”(a literal “with” quotes (and parens))”
 Base syntax is a literal ‘R’ followed by a double quote then a parenthese.
 String is terminated with parenthese and a double quote.
 Extra delimiter characters can be put between the “ and (.
 The string is then terminated only with the same character sequence.
auto str = R”ats(an “ats” quoted string with one )ats”;
 ‘R’ can be prefixed with “u8”, “u”, “U”, or “L” for different encodings.
Apache Traffic Server Summit Fall 2016 37
std::array<T, N>
 Equivalent to C array.
 Fixed size.
 Single chunk of memory.
 N must be a positive integral value.
 Makes an array easier to use with other C++ features
 E.g. range for loops.
Apache Traffic Server Summit Fall 2016 38
Old Stuff
Apache Traffic Server Summit Fall 2016 39
Casting
 Not new to C++11 but time to switch to actual C++ style
 Don’t use C style casts.
 C style cast can do different things depending on the mood of the compiler.
 C++ casts make it clear what is being done.
 static_cast
 dynamic_cast
 const_cast
 reinterpret_cast
 Yes, it can look ugly. But it’s good for you.
Apache Traffic Server Summit Fall 2016 40
Anonymous namespace
 Use of static to prevent external linkage is deprecated.
 Use the anonymous namespace instead.
namespace {
const char PLUGIN_NAME[] = “sorberizer”;
}
Apache Traffic Server Summit Fall 2016 41
Inline functions
 Inline functions are almost always a better choice than #define.
 Argument type checking.
 No order of evaluation or multiple evaluation issues.
 Control of namespace collisions
 Even better with constexpr to get compile time constant folding.
Apache Traffic Server Summit Fall 2016 42

More Related Content

What's hot

End to-end async and await
End to-end async and awaitEnd to-end async and await
End to-end async and awaitvfabro
 
C# 5 deep drive into asynchronous programming
C# 5 deep drive into asynchronous programmingC# 5 deep drive into asynchronous programming
C# 5 deep drive into asynchronous programmingPraveen Prajapati
 
Treasure Data Summer Internship 2016
Treasure Data Summer Internship 2016Treasure Data Summer Internship 2016
Treasure Data Summer Internship 2016Yuta Iwama
 
Infrastructure & System Monitoring using Prometheus
Infrastructure & System Monitoring using PrometheusInfrastructure & System Monitoring using Prometheus
Infrastructure & System Monitoring using PrometheusMarco Pas
 
Kafka monitoring and metrics
Kafka monitoring and metricsKafka monitoring and metrics
Kafka monitoring and metricsTouraj Ebrahimi
 
Treasure Data Summer Internship Final Report
Treasure Data Summer Internship Final ReportTreasure Data Summer Internship Final Report
Treasure Data Summer Internship Final ReportRitta Narita
 
Async in .NET
Async in .NETAsync in .NET
Async in .NETRTigger
 
CTU June 2011 - C# 5.0 - ASYNC & Await
CTU June 2011 - C# 5.0 - ASYNC & AwaitCTU June 2011 - C# 5.0 - ASYNC & Await
CTU June 2011 - C# 5.0 - ASYNC & AwaitSpiffy
 
Performance Optimization of Rails Applications
Performance Optimization of Rails ApplicationsPerformance Optimization of Rails Applications
Performance Optimization of Rails ApplicationsSerge Smetana
 
Top 10 Perl Performance Tips
Top 10 Perl Performance TipsTop 10 Perl Performance Tips
Top 10 Perl Performance TipsPerrin Harkins
 
Rails Application Optimization Techniques & Tools
Rails Application Optimization Techniques & ToolsRails Application Optimization Techniques & Tools
Rails Application Optimization Techniques & Toolsguest05c09d
 
Monitoring Kafka w/ Prometheus
Monitoring Kafka w/ PrometheusMonitoring Kafka w/ Prometheus
Monitoring Kafka w/ Prometheuskawamuray
 
Binary Studio Academy: Concurrency in C# 5.0
Binary Studio Academy: Concurrency in C# 5.0Binary Studio Academy: Concurrency in C# 5.0
Binary Studio Academy: Concurrency in C# 5.0Binary Studio
 
Reactive programming with examples
Reactive programming with examplesReactive programming with examples
Reactive programming with examplesPeter Lawrey
 
Reactive programming with Rxjava
Reactive programming with RxjavaReactive programming with Rxjava
Reactive programming with RxjavaChristophe Marchal
 
Low latency in java 8 by Peter Lawrey
Low latency in java 8 by Peter Lawrey Low latency in java 8 by Peter Lawrey
Low latency in java 8 by Peter Lawrey J On The Beach
 
Heat optimization
Heat optimizationHeat optimization
Heat optimizationRico Lin
 

What's hot (20)

Async await
Async awaitAsync await
Async await
 
End to-end async and await
End to-end async and awaitEnd to-end async and await
End to-end async and await
 
C# 5 deep drive into asynchronous programming
C# 5 deep drive into asynchronous programmingC# 5 deep drive into asynchronous programming
C# 5 deep drive into asynchronous programming
 
Treasure Data Summer Internship 2016
Treasure Data Summer Internship 2016Treasure Data Summer Internship 2016
Treasure Data Summer Internship 2016
 
Async/Await
Async/AwaitAsync/Await
Async/Await
 
Infrastructure & System Monitoring using Prometheus
Infrastructure & System Monitoring using PrometheusInfrastructure & System Monitoring using Prometheus
Infrastructure & System Monitoring using Prometheus
 
Kafka monitoring and metrics
Kafka monitoring and metricsKafka monitoring and metrics
Kafka monitoring and metrics
 
Treasure Data Summer Internship Final Report
Treasure Data Summer Internship Final ReportTreasure Data Summer Internship Final Report
Treasure Data Summer Internship Final Report
 
Async in .NET
Async in .NETAsync in .NET
Async in .NET
 
Async Programming in C# 5
Async Programming in C# 5Async Programming in C# 5
Async Programming in C# 5
 
CTU June 2011 - C# 5.0 - ASYNC & Await
CTU June 2011 - C# 5.0 - ASYNC & AwaitCTU June 2011 - C# 5.0 - ASYNC & Await
CTU June 2011 - C# 5.0 - ASYNC & Await
 
Performance Optimization of Rails Applications
Performance Optimization of Rails ApplicationsPerformance Optimization of Rails Applications
Performance Optimization of Rails Applications
 
Top 10 Perl Performance Tips
Top 10 Perl Performance TipsTop 10 Perl Performance Tips
Top 10 Perl Performance Tips
 
Rails Application Optimization Techniques & Tools
Rails Application Optimization Techniques & ToolsRails Application Optimization Techniques & Tools
Rails Application Optimization Techniques & Tools
 
Monitoring Kafka w/ Prometheus
Monitoring Kafka w/ PrometheusMonitoring Kafka w/ Prometheus
Monitoring Kafka w/ Prometheus
 
Binary Studio Academy: Concurrency in C# 5.0
Binary Studio Academy: Concurrency in C# 5.0Binary Studio Academy: Concurrency in C# 5.0
Binary Studio Academy: Concurrency in C# 5.0
 
Reactive programming with examples
Reactive programming with examplesReactive programming with examples
Reactive programming with examples
 
Reactive programming with Rxjava
Reactive programming with RxjavaReactive programming with Rxjava
Reactive programming with Rxjava
 
Low latency in java 8 by Peter Lawrey
Low latency in java 8 by Peter Lawrey Low latency in java 8 by Peter Lawrey
Low latency in java 8 by Peter Lawrey
 
Heat optimization
Heat optimizationHeat optimization
Heat optimization
 

Viewers also liked

Apache Traffic Server
Apache Traffic ServerApache Traffic Server
Apache Traffic Serversupertom
 
Apache trafficserver
Apache trafficserverApache trafficserver
Apache trafficserverDin Dindin
 
Apache Traffic Server Internals
Apache Traffic Server InternalsApache Traffic Server Internals
Apache Traffic Server InternalsPSUdaemon
 
Introduction to ATS plugins
Introduction to ATS pluginsIntroduction to ATS plugins
Introduction to ATS pluginsPSUdaemon
 
H2O - making the Web faster
H2O - making the Web fasterH2O - making the Web faster
H2O - making the Web fasterKazuho Oku
 

Viewers also liked (6)

ATS Internals
ATS InternalsATS Internals
ATS Internals
 
Apache Traffic Server
Apache Traffic ServerApache Traffic Server
Apache Traffic Server
 
Apache trafficserver
Apache trafficserverApache trafficserver
Apache trafficserver
 
Apache Traffic Server Internals
Apache Traffic Server InternalsApache Traffic Server Internals
Apache Traffic Server Internals
 
Introduction to ATS plugins
Introduction to ATS pluginsIntroduction to ATS plugins
Introduction to ATS plugins
 
H2O - making the Web faster
H2O - making the Web fasterH2O - making the Web faster
H2O - making the Web faster
 

Similar to Gude for C++11 in Apache Traffic Server

A Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its APIA Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its APIJörn Guy Süß JGS
 
whats new in java 8
whats new in java 8 whats new in java 8
whats new in java 8 Dori Waldman
 
Java 8 - An Overview
Java 8 - An OverviewJava 8 - An Overview
Java 8 - An OverviewIndrajit Das
 
Whats New in Visual Studio 2012 for C++ Developers
Whats New in Visual Studio 2012 for C++ DevelopersWhats New in Visual Studio 2012 for C++ Developers
Whats New in Visual Studio 2012 for C++ DevelopersRainer Stropek
 
Typescript language extension of java script
Typescript language extension of java scriptTypescript language extension of java script
Typescript language extension of java scriptmichaelaaron25322
 
Python Programming Basics for begginners
Python Programming Basics for begginnersPython Programming Basics for begginners
Python Programming Basics for begginnersAbishek Purushothaman
 
The Little Wonders of C# 6
The Little Wonders of C# 6The Little Wonders of C# 6
The Little Wonders of C# 6BlackRabbitCoder
 
Java8 training - Class 1
Java8 training  - Class 1Java8 training  - Class 1
Java8 training - Class 1Marut Singh
 
Type script - advanced usage and practices
Type script  - advanced usage and practicesType script  - advanced usage and practices
Type script - advanced usage and practicesIwan van der Kleijn
 
Java8: what's new and what's hot
Java8: what's new and what's hotJava8: what's new and what's hot
Java8: what's new and what's hotSergii Maliarov
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentationVan Huong
 
Cpp17 and Beyond
Cpp17 and BeyondCpp17 and Beyond
Cpp17 and BeyondComicSansMS
 
Introduction to c_plus_plus
Introduction to c_plus_plusIntroduction to c_plus_plus
Introduction to c_plus_plusSayed Ahmed
 
Introduction to c_plus_plus (6)
Introduction to c_plus_plus (6)Introduction to c_plus_plus (6)
Introduction to c_plus_plus (6)Sayed Ahmed
 
Can't Dance The Lambda
Can't Dance The LambdaCan't Dance The Lambda
Can't Dance The LambdaTogakangaroo
 

Similar to Gude for C++11 in Apache Traffic Server (20)

The Style of C++ 11
The Style of C++ 11The Style of C++ 11
The Style of C++ 11
 
A Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its APIA Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its API
 
Cs30 New
Cs30 NewCs30 New
Cs30 New
 
Lambdas puzzler - Peter Lawrey
Lambdas puzzler - Peter LawreyLambdas puzzler - Peter Lawrey
Lambdas puzzler - Peter Lawrey
 
whats new in java 8
whats new in java 8 whats new in java 8
whats new in java 8
 
Java 8 - An Overview
Java 8 - An OverviewJava 8 - An Overview
Java 8 - An Overview
 
Whats New in Visual Studio 2012 for C++ Developers
Whats New in Visual Studio 2012 for C++ DevelopersWhats New in Visual Studio 2012 for C++ Developers
Whats New in Visual Studio 2012 for C++ Developers
 
Typescript language extension of java script
Typescript language extension of java scriptTypescript language extension of java script
Typescript language extension of java script
 
Python Programming Basics for begginners
Python Programming Basics for begginnersPython Programming Basics for begginners
Python Programming Basics for begginners
 
The Little Wonders of C# 6
The Little Wonders of C# 6The Little Wonders of C# 6
The Little Wonders of C# 6
 
Java8 training - Class 1
Java8 training  - Class 1Java8 training  - Class 1
Java8 training - Class 1
 
Type script - advanced usage and practices
Type script  - advanced usage and practicesType script  - advanced usage and practices
Type script - advanced usage and practices
 
Java8: what's new and what's hot
Java8: what's new and what's hotJava8: what's new and what's hot
Java8: what's new and what's hot
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentation
 
LLVM
LLVMLLVM
LLVM
 
cp05.pptx
cp05.pptxcp05.pptx
cp05.pptx
 
Cpp17 and Beyond
Cpp17 and BeyondCpp17 and Beyond
Cpp17 and Beyond
 
Introduction to c_plus_plus
Introduction to c_plus_plusIntroduction to c_plus_plus
Introduction to c_plus_plus
 
Introduction to c_plus_plus (6)
Introduction to c_plus_plus (6)Introduction to c_plus_plus (6)
Introduction to c_plus_plus (6)
 
Can't Dance The Lambda
Can't Dance The LambdaCan't Dance The Lambda
Can't Dance The Lambda
 

Recently uploaded

notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptMsecMca
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startQuintin Balsdon
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoordharasingh5698
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringmulugeta48
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxJuliansyahHarahap1
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdfKamal Acharya
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfRagavanV2
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapRishantSharmaFr
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationBhangaleSonal
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityMorshed Ahmed Rahath
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 

Recently uploaded (20)

notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 

Gude for C++11 in Apache Traffic Server

  • 1. C++ Eleventy for ATS Style and Suggestions
  • 2. Introduction  C++11 (“eleventy”) is a major change in the language (Lisp rulz!)  7.0 release requires compilers that are C++11 compliant.  Take advantage of new language features without going crazy.  You know who you are.  Focus here is on ATS related features, not an overview of all changes.  Looking at just the features I think are the most useful for ATS coding. Apache Traffic Server Summit Fall 2016 2
  • 3. nullptr  A null pointer.  Never worse and frequently better than  NULL  0  nullptr is of type nullptr_t - overloads are distinct from ‘0’ or NULL.  Classes that involve pointers and numbers (e.g., strings) can now differentiate ‘0’ as “zero” vs. “null”.  This is required.  Do not use NULL.  Should not use ‘0’ either to mean NULL. Apache Traffic Server Summit Fall 2016 3
  • 4. auto Make the compiler figure out the type  Useful to avoid having to write a type twice.  auto thing = new Thing;  Types it would take days to figure out and don’t really matter.  Iterators, complex containers, lambdas.  Local uses where the type is unimportant  locally scoped variables.  { auto tmp = lhs; lhs = rhs; rhs = tmp; }  for loop variables (but see later section) Apache Traffic Server Summit Fall 2016 4
  • 5. auto  Qualifiers can be added to the computed type.  const, volatile, lvalue reference, rvalue reference, pointer.  Requires an initialization expression from which to deduce the type. Apache Traffic Server Summit Fall 2016 5
  • 6. auto Don’t drive others crazy  Easy to overuse.  Don’t make people guess about the type.  Should be obvious or irrelevant.  Be extra careful with numerics.  The compiler doesn’t always think it’s the same type you do.  In many cases in ATS the numeric type needs to be very specific. Apache Traffic Server Summit Fall 2016 6
  • 7. Range-based for loops  Iterate over elements in a container. for (auto& item : acl_list) { item.update(); // … }  Container must support C++ standard iteration via  begin / end methods  begin / end functions  An array with a declared length.  The variable for elements can be a value which will be a copy of the actual element.  The variable does not have to be declared auto but that’s easier. Apache Traffic Server Summit Fall 2016 7
  • 8. Methodoloy Useful advancements in method technology Apache Traffic Server Summit Fall 2016 8
  • 9. Delegating Constructors  init() methods are error prone and annoying.  C++11 makes it possible to call a constructor from another constructor using member initialization syntax. class Leif { int a; public: Leif(int x) { if (0<x && x<=max) a=x; else throw bad_Leif(x); } Leif() : Leif{42} { } Leif(string s) : Leif{lexical_cast<int>(s)} { } // ... }; Apache Traffic Server Summit Fall 2016 9
  • 10. Prohibiting methods  Methods can be explicitly removed from the class. class X { X& operator=(const X&) = delete; // Disallow copying X(const X&) = delete; };  Use this instead of declaring but not defining these methods.  This works for any method including inherited methods. Apache Traffic Server Summit Fall 2016 10
  • 11. Defaulting methods  Methods can be defined as “default” which means use the compiler generated method.  Can only be used for methods that have compiler definitions.  Theoretically not useful because doing nothing has the same effect.  Always use instead of defining a method that replicates default behavior.  Make it explicit the default is being used. Apache Traffic Server Summit Fall 2016 11
  • 12. Member Initialization  Class members can be initialized directly in the class declaration.  Overridden by constructor  Avoid unobvious repetition in multiple constructors Class Leif { Leif() : left(2), right(3), hair(HAT), phrase(“kill it with fire”), yet(4), another(5), member(7) {} Leif(const char *text) : left(2), right(3), hair(HAT), phrase(text), yet(4), another(5), member(7) {} }; // vs. class Leif { Leif() {} Leif(char const* text) : phrase(text) {} int left = 2; int right = 3; HairStyle hair(HAT); // … }; Apache Traffic Server Summit Fall 2016 12
  • 13. EThread::EThread() : generator((uint64_t)ink_get_hrtime_internal() ^ (uint64_t)(uintptr_t) this), ethreads_to_be_signalled(NULL), n_ethreads_to_be_signalled(0), main_accept_index(-1), id(NO_ETHREAD_ID), event_types(0), tt(REGULAR), current_metric(metrics + (ink_get_hrtime_internal() / HRTIME_SECOND) % N_EVENT_METRICS) { memset(thread_private, 0, PER_THREAD_DATA); } EThread::EThread(ThreadType att, int anid) : generator((uint64_t)ink_get_hrtime_internal() ^ (uint64_t)(uintptr_t) this), ethreads_to_be_signalled(NULL), n_ethreads_to_be_signalled(0), main_accept_index(-1), id(anid), event_types(0), tt(att), server_session_pool(NULL), current_metric(metrics + (ink_get_hrtime_internal() / HRTIME_SECOND) % N_EVENT_METRICS) { } EThread::EThread(ThreadType att, Event *e) : generator((uint32_t)((uintptr_t)time(NULL) ^ (uintptr_t) this)), ethreads_to_be_signalled(NULL), n_ethreads_to_be_signalled(0), main_accept_index(-1), id(NO_ETHREAD_ID), event_types(0), tt(att), oneevent(e), current_metric(metrics + (ink_get_hrtime_internal() / HRTIME_SECOND) % N_EVENT_METRICS) { ink_assert(att == DEDICATED); memset(thread_private, 0, PER_THREAD_DATA); } Apache Traffic Server Summit Fall 2016 13
  • 14. class EThread : public Thread { // ... The initialized members InkRand generator = static_cast<uint64_t>(Thread::get_hrtime_updated() ^ reinterpret_cast<uintptr_t>(this)); EThread **ethreads_to_be_signalled = nullptr; int n_ethreads_to_be_signalled = 0; unsigned int event_types = 0; Event *start_event = nullptr; ServerSessionPool *server_session_pool = nullptr; }; EThread::EThread() : id(NO_ETHREAD_ID), tt(REGULAR) { memset(thread_private, 0, P ER_THREAD_DATA); } EThread::EThread(ThreadType att, int anid) : id(anid), tt(att), server_session_pool(nullptr) { // … } EThread::EThread(ThreadType att, Event *e) : id(NO_ETHREAD_ID), tt(att), start_event(e) { memset(thread_private, 0, PER_THREAD_DATA); } Apache Traffic Server Summit Fall 2016 14
  • 15. override  Marks a method as overriding a super class method.  No code effect.  Useful to be sure the method really does override.  Recommended. Apache Traffic Server Summit Fall 2016 15
  • 16. Explicit conversion operators  User type conversions can be marked explicit.  This allows direct conversions but not implicit conversions.  Classic example – conversion to bool.  In C++98 this also allows further conversion to pointer or numeric types.  Worked around by making the conversion to a pointer to method.  In C++11 mark conversion explicit. Apache Traffic Server Summit Fall 2016 16
  • 17. More stuff Apache Traffic Server Summit Fall 2016 17
  • 18. std::unique_ptr<T> #include <memory>  Dynamically allocated object that is not shared.  Default constructed to contain nullptr.  When the instance is destroyed or has another value assigned it is cleaned up.  Clean up mechanism can be changed, defaults to the destructor for T.  ats_scoped_XXX is modeled on this class.  Change should be easy for this reason.  Always intended as a temporary measure until we could use C++ eleventy. Apache Traffic Server Summit Fall 2016 18
  • 19. std::unique_ptr<T>  Useful for  Class members that cannot be statically declared but share the lifetime of the class.  Local temporary heap storage. Apache Traffic Server Summit Fall 2016 19
  • 20. Lambda  Lambda is a notation that creates an anonymous functor class and instantiates it.  [] () mutable throw() -> T { /* … */ } capture parameters optional optional return type code  Common functionality in ECMA style languages (Perl, Python, JavaScript, …)  Lambdas can be used as temporaries or stored  Via auto or std::function.  It’s C++, both the lambda and its container are strongly typed.  Lambdas can bind objects in current scope either by value or reference.  Be careful of variable lifetime vs. lambda lifetime.  Value binding vs. mutable – by default values are const. Apache Traffic Server Summit Fall 2016 20
  • 21. Lambda Capture  Capture syntax basic  “[=]” -> capture by value.  “[&]” -> capture by reference.  These capture any name used in that lambda that is not in the lambda scope but in the enclosing scope.  Capture syntax by name  “[=name]” -> capture “name” by value.  “[&, =thing1, =thing2]” -> capture all names in use by reference except “thing1” and “thing2” which are captured by value. Apache Traffic Server Summit Fall 2016 21
  • 22. Lambda curry  Lambdas can be used to curry and bind functions.  Create shims between the function you have and what the API needs.  Change the set of parameters or fix parameters to specific values.  Create a function object from an object instance and a method.  Make Standard Template Library algorithms much more useful. Apache Traffic Server Summit Fall 2016 22
  • 23. Example typedef std::function<void (const char*)> CallBack; void SetCallback(CallBack cb); int some_function(const unsigned char* input) { … } // … some other code site, convert some_function to expected callback form SetCallBack(lambda [] (const char* s) -> void { (void) some_function(reinterpret_cast<const unsigned char*>(s); } Apache Traffic Server Summit Fall 2016 23
  • 24. std::function #include <functional>  Holds an object that can be called as a function.  Function, functor, lambda.  Defined with a specific signature.  Simplifies providing flexibility to caller.  Good for global or process start callbacks.  Can be used with lambdas to curry functions or pass method callbacks.  NOTE: This stores the function pointer only. If bound to a method it does not preserve the object.  May require allocation if passed a functor or lambda. Apache Traffic Server Summit Fall 2016 24
  • 25. constexpr  Marks a variable, method, or function as compile time computable.  For variables, used in place of const.  Static methods work as functions.  Dynamic methods work if class instance used is constexpr.  Opposite of taint, “pure”.  If all inputs are constexpr then result is constexpr.  If any input is tainted (not constexpr) neither is result.  Can be used in non-compile time constant context.  Purpose – do more general and complex compile time computations. Apache Traffic Server Summit Fall 2016 25
  • 26. constexpr  Consider a simple computation like # define BUFFER_SIZE_FOR_INDEX(i) (DEFAULT_BUFFER_BASE_SIZE * (1 << (i)))  This is reduced to a constant during compilation. If an inline function were used inline size_t BUFFER_SIZE_FOR_INDEX(int i) { return DEFAULT_BUFFER_BASE_SIZE * (1<< i)); }  This would be cleaner but no longer always a constant. constexpr makes it possible to have the best of defines and inline functions. inline constexpr size_t BUFFER_SIZE_FOR_INDEX(int i) { … } Apache Traffic Server Summit Fall 2016 26
  • 27. static_assert  Compile time assertion.  Two arguments, a constexpr and a string.  If the expression is not true, the compilation fails and the string is printed.  Equivalent used many places in ATS, either done dynamically or via singleton constructor.  Can use type information unlike #if. static_assert(INK_START_ERRNO > SOCK_ERROR, “Internal errono values overlap system errno values”); Apache Traffic Server Summit Fall 2016 27
  • 28. Rvalue reference  New qualifier – rvalue reference. “int&& rvr = 4;”  lvalue vs. rvalue  lvalue has a name / specified by programmer.  rvalue has no name / created by compiler (“temporary”).  “Universal reference”  “&&” can mean either lvalue or rvalue reference if the type it qualifies is deduced by compiler.  Syntactic sugar only – all actual instantiations are lvalue or rvalue reference. Apache Traffic Server Summit Fall 2016 28
  • 29. Perfect Forwarding  Addition of rvalue and “universal” references allows a function F to forward its arguments exactly to another function.  Root cause – temporaries vs. variables.  Previously this took 2N template implementations for an N parameter function.  Technically possible but rarely, if ever, done in practice.  The key trick is a “universal reference” can evolve in to an lvalue or rvalue reference as needed to forward the argument.  This is an obscure technique – if it’s not clear, don’t try to use it. Apache Traffic Server Summit Fall 2016 29
  • 30. Move  “Move semantics” is a combination of  Bitwise copy  Destructor skipping  Object A is bitwise copied to object B without the destructor or constructor of A or B invoked.  Useful in specific situations where the contents of A are important but neither A itself nor the contents of B are.  But those situations show up very frequently.  B is a default constructed container element.  B is a return value.  B is raw memory that has never been constructed. Apache Traffic Server Summit Fall 2016 30
  • 31. std::move  Template formalization to provide additional control of movement.  Specialize if needed for specific types.  When used it should be expected to result in the destruction of the object.  In many ways like auto_ptr but not done stupid. // Fast container add that avoids a copy Thing q; Container c; c.emplace(std::move(q)); // assume q is invalid at this point. Apache Traffic Server Summit Fall 2016 31
  • 32. Enum class, storage  Enumerations can be classes.  Creates type difference from numeric types.  Constrains name space injection.  Good for global / file scope enums.  Class nested enums should be plain.  Enums can specific storage class.  Optimize memory footprint.  Allow forward declaration / in name only. Apache Traffic Server Summit Fall 2016 32
  • 33. Uniform initialization  Bit of a misnomer but allows a wide variety of initializations with the same syntax.  Aggregate initialization.  Constructor invocation.  Uniform initialization can never be mistaken for a function definition.  POD without a constructor is considered an aggregate.  For PODs with few members very handy. Pass temporary without writing a trivial constructor.  Notation is recursive. Apache Traffic Server Summit Fall 2016 33
  • 34. Uniform initialization  Can be overridden with constructor.  Single argument of type std::initializer_list<T>.  Note the list must be homogenous.  Can be used in container loops. for ( auto item const& : { “one”, “two”, “three” } ) { … }  This can avoid having to declare an array used in only one loop. Apache Traffic Server Summit Fall 2016 34
  • 35. Uniform initialization examples struct Leif { int q; const char* phrase; }; void f() { Leif l = Leif{1, “ack”}; Leif l{1, “ack”}; Leif clones[4] = { {1, “ack”}, {2, “make it so”},{3, “pebkac”},{4, “use lua!”}}; } Apache Traffic Server Summit Fall 2016 35
  • 36. Uniform Initialization – another example struct Exile { bool deposed_p, const char* name, const char* reason; }; void make_soylent_green(Exile&& victim); make_soylent_green(Exile { true, “Leif”, “too much hat wearing” }); // or even make_soylent_green({ true, “Leif”, “too much hat wearing”}); Apache Traffic Server Summit Fall 2016 36
  • 37. Raw Strings  Raw string literals allow alternate string delimiters to simplify escaping. auto str = R”(a literal “with” quotes (and parens))”  Base syntax is a literal ‘R’ followed by a double quote then a parenthese.  String is terminated with parenthese and a double quote.  Extra delimiter characters can be put between the “ and (.  The string is then terminated only with the same character sequence. auto str = R”ats(an “ats” quoted string with one )ats”;  ‘R’ can be prefixed with “u8”, “u”, “U”, or “L” for different encodings. Apache Traffic Server Summit Fall 2016 37
  • 38. std::array<T, N>  Equivalent to C array.  Fixed size.  Single chunk of memory.  N must be a positive integral value.  Makes an array easier to use with other C++ features  E.g. range for loops. Apache Traffic Server Summit Fall 2016 38
  • 39. Old Stuff Apache Traffic Server Summit Fall 2016 39
  • 40. Casting  Not new to C++11 but time to switch to actual C++ style  Don’t use C style casts.  C style cast can do different things depending on the mood of the compiler.  C++ casts make it clear what is being done.  static_cast  dynamic_cast  const_cast  reinterpret_cast  Yes, it can look ugly. But it’s good for you. Apache Traffic Server Summit Fall 2016 40
  • 41. Anonymous namespace  Use of static to prevent external linkage is deprecated.  Use the anonymous namespace instead. namespace { const char PLUGIN_NAME[] = “sorberizer”; } Apache Traffic Server Summit Fall 2016 41
  • 42. Inline functions  Inline functions are almost always a better choice than #define.  Argument type checking.  No order of evaluation or multiple evaluation issues.  Control of namespace collisions  Even better with constexpr to get compile time constant folding. Apache Traffic Server Summit Fall 2016 42

Editor's Notes

  1. Note the variable are just as strongly typed as before, it’s just saving some typing.
  2. Value vs. reference depends on the context.
  3. Technical note: Probably need to update the records API to handle unique_ptr<char> to avoid bypassing accessor methods.
  4. Need to check on std::forward to make sure that’s the actual template.
  5. Can’t inherit from an enum class. Non-class can specify storage size.
  6. Note – for POD the members must be public, in addition to no constructor.