SlideShare a Scribd company logo
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

PPTX
AMC Minor Technical Issues
PDF
HTTP/2 Server Push
ODP
Using Grails to power your electric car
PDF
Monitoring infrastructure with prometheus
PDF
Puppet Camp DC 2015: Distributed OpenSCAP Compliance Validation with MCollective
PPTX
Async and Await on the Server
PPSX
Async-await best practices in 10 minutes
PDF
Netty from the trenches
AMC Minor Technical Issues
HTTP/2 Server Push
Using Grails to power your electric car
Monitoring infrastructure with prometheus
Puppet Camp DC 2015: Distributed OpenSCAP Compliance Validation with MCollective
Async and Await on the Server
Async-await best practices in 10 minutes
Netty from the trenches

What's hot (20)

PPTX
Async await
PPTX
End to-end async and await
PPTX
C# 5 deep drive into asynchronous programming
PDF
Treasure Data Summer Internship 2016
PPTX
Async/Await
PDF
Infrastructure & System Monitoring using Prometheus
PDF
Kafka monitoring and metrics
PDF
Treasure Data Summer Internship Final Report
PPTX
Async in .NET
PPTX
Async Programming in C# 5
PPTX
CTU June 2011 - C# 5.0 - ASYNC & Await
ODP
Performance Optimization of Rails Applications
PDF
Top 10 Perl Performance Tips
PDF
Rails Application Optimization Techniques & Tools
PDF
Monitoring Kafka w/ Prometheus
PPTX
Binary Studio Academy: Concurrency in C# 5.0
PPT
Reactive programming with examples
PDF
Reactive programming with Rxjava
PPTX
Low latency in java 8 by Peter Lawrey
PDF
Heat optimization
Async await
End to-end async and await
C# 5 deep drive into asynchronous programming
Treasure Data Summer Internship 2016
Async/Await
Infrastructure & System Monitoring using Prometheus
Kafka monitoring and metrics
Treasure Data Summer Internship Final Report
Async in .NET
Async Programming in C# 5
CTU June 2011 - C# 5.0 - ASYNC & Await
Performance Optimization of Rails Applications
Top 10 Perl Performance Tips
Rails Application Optimization Techniques & Tools
Monitoring Kafka w/ Prometheus
Binary Studio Academy: Concurrency in C# 5.0
Reactive programming with examples
Reactive programming with Rxjava
Low latency in java 8 by Peter Lawrey
Heat optimization
Ad

Viewers also liked (6)

PPTX
ATS Internals
PPT
Apache Traffic Server
PDF
Apache trafficserver
PDF
Apache Traffic Server Internals
PDF
Introduction to ATS plugins
PDF
H2O - making the Web faster
ATS Internals
Apache Traffic Server
Apache trafficserver
Apache Traffic Server Internals
Introduction to ATS plugins
H2O - making the Web faster
Ad

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

PPT
Gentle introduction to modern C++
PPT
What's New in C++ 11?
PDF
C++ How to Program 10th Edition Deitel Test Bank
PPTX
How to Adopt Modern C++17 into Your C++ Code
PPTX
How to Adopt Modern C++17 into Your C++ Code
PPTX
Modern C++ Lunch and Learn
PPTX
C++ Core Guidelines
PPTX
Modern C++
PDF
How to make a large C++-code base manageable
PPT
Stroustrup c++0x overview
PPTX
From code to pattern, part one
PPTX
C++ idioms.pptx
PDF
Getting Started with C++ (TCF 2014)
PPTX
C traps and pitfalls for C++ programmers
PDF
The c++coreguidelinesforsavercode
PPTX
PDF
Computer_Programming_Part_II_Segment_02.pdf
PPTX
Whats New in Visual Studio 2012 for C++ Developers
PDF
Modern c++ (C++ 11/14)
PDF
Getting started with C++
Gentle introduction to modern C++
What's New in C++ 11?
C++ How to Program 10th Edition Deitel Test Bank
How to Adopt Modern C++17 into Your C++ Code
How to Adopt Modern C++17 into Your C++ Code
Modern C++ Lunch and Learn
C++ Core Guidelines
Modern C++
How to make a large C++-code base manageable
Stroustrup c++0x overview
From code to pattern, part one
C++ idioms.pptx
Getting Started with C++ (TCF 2014)
C traps and pitfalls for C++ programmers
The c++coreguidelinesforsavercode
Computer_Programming_Part_II_Segment_02.pdf
Whats New in Visual Studio 2012 for C++ Developers
Modern c++ (C++ 11/14)
Getting started with C++

Recently uploaded (20)

PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
PPT on Performance Review to get promotions
PPTX
Geodesy 1.pptx...............................................
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPT
Project quality management in manufacturing
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
composite construction of structures.pdf
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PDF
Well-logging-methods_new................
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
Lecture Notes Electrical Wiring System Components
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
Construction Project Organization Group 2.pptx
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
web development for engineering and engineering
CH1 Production IntroductoryConcepts.pptx
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPT on Performance Review to get promotions
Geodesy 1.pptx...............................................
Foundation to blockchain - A guide to Blockchain Tech
Project quality management in manufacturing
UNIT 4 Total Quality Management .pptx
UNIT-1 - COAL BASED THERMAL POWER PLANTS
composite construction of structures.pdf
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Well-logging-methods_new................
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Lecture Notes Electrical Wiring System Components
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Construction Project Organization Group 2.pptx
Model Code of Practice - Construction Work - 21102022 .pdf
web development for engineering and engineering

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

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