John Fearnside
john@moz.com
C++ isn't much like Ruby...
and never will be...
but it's getting a little closer
C++98 First ISO standard
C++03 Bug fix
C++11 First major revision (13 years later)
Approved 12 August 2011
Many features have been available in major compilers for a while
Existing code still works
Compiler support relatively new
New programmers leary
Not your father's C++
Old guidance doesn't necessarily apply
size_t frameViewsCount = frameViews.size(); for (size_t i = 0; i < frameViewsCount; ++i) frameViews[i]->updateLayoutAndStyleIfNeededRecursive();
Wrong:
const Vector<RefPtr<FrameView> >::iterator end = frameViews.end(); for (Vector<RefPtr<FrameView> >::iterator it = frameViews.begin(); it != end; ++it) (*it)->updateLayoutAndStyleIfNeededRecursive();
we'll come back to this
C++ can be simpler, like Ruby
Array in Ruby
std::vector in C++
v.rb
v = [-99, 0, 42, 3, -1, 6] v.each do |iter| print "#{iter} " end print "\n"
$ ruby v.rb -99 0 42 3 -1 6
v0.cpp
#include <iostream> #include <vector> int main() { int arr[] = {-99, 0, 42, 3, -1, 6}; std::vector<int> v(arr, arr + sizeof(arr) / sizeof(int)); std::vector<int>::iterator end = v.end(); std::vector<int>::iterator iter = v.begin(); for (; iter != end; ++iter) { *iter = 0; std::cout << *iter << " "; } std::cout << std::endl; return 0; }
$ ./run v0.cpp 0 0 0 0 0 0
v0.cpp
#include <iostream> #include <vector> int main() { int arr[] = {-99, 0, 42, 3, -1, 6}; std::vector<int> v(arr, arr + sizeof(arr) / sizeof(int)); std::vector<int>::const_iterator end = v.end(); std::vector<int>::const_iterator iter = v.begin(); for (; iter != end; ++iter) { *iter = 0; std::cout << *iter << " "; } std::cout << std::endl; return 0; }
$ ./run v0.cpp v0.cpp:10:11: error: read-only variable is not assignable *iter = 0; ~~~~~ ^ 1 error generated.
v0.cpp
#include <iostream> #include <vector> int main() { int arr[] = {-99, 0, 42, 3, -1, 6}; std::vector<int> v(arr, arr + sizeof(arr) / sizeof(int)); std::vector<int>::const_iterator end = v.end(); std::vector<int>::const_iterator iter = v.begin(); for (; iter != end; ++iter) { std::cout << *iter << " "; } std::cout << std::endl; return 0; }
$ ./run v0.cpp -99 0 42 3 -1 6
v1.cpp
#include <iostream> #include <vector> int main() { //... std::cout << std::endl; return 0; }
Initialization Lists
Uniform Initialization
C++98:
int arr[] = {-99, 0, 42, 3, -1, 6}; std::vector<int> v(arr, arr + sizeof(arr) / sizeof(int));
C++98:
int arr[] = {-99, 0, 42, 3, -1, 6}; std::vector<int> v(arr, arr + sizeof(arr) / sizeof(int));
C++11:
std::vector<int> v{-99, 0, 42, 3, -1, 6};
Auto
C++98:
std::vector<int>::const_iterator iter = v.begin();
C++98:
std::vector<int>::const_iterator iter = v.begin();
C++11:
auto iter = v.begin();
Range-based for
C++98:
std::vector<int>::const_iterator end = v.end(); std::vector<int>::const_iterator iter = v.begin(); for (; iter != end; ++iter) {
C++98:
std::vector<int>::const_iterator end = v.end(); std::vector<int>::const_iterator iter = v.begin(); for (; iter != end; ++iter) {
C++11:
for (auto iter : v) {
std::vector<int> v{-99, 0, 42, 3, -1, 6}; for (auto iter : v) { iter = 0; std::cout << iter << " "; } std::cout << std::endl;
$ ./run v1.cpp 0 0 0 0 0 0
std::vector<int> v{-99, 0, 42, 3, -1, 6}; for (const auto & iter : v) { iter = 0; std::cout << iter << " "; } std::cout << std::endl;
$ ./run v1.cpp v1.cpp:7:9: error: read-only variable is not assignable iter = 0; ~~~~ ^ 1 error generated.
std::vector<int> v{-99, 0, 42, 3, -1, 6}; for (const auto iter : v) { std::cout << iter << " "; } std::cout << std::endl;
$ ./run v1.cpp -99 0 42 3 -1 6
C++11:
std::vector<int> v{-99, 0, 42, 3, -1, 6}; for (const auto & iter : v) { std::cout << iter << " "; } std::cout << std::endl;
Ruby:
v = [-99, 0, 42, 3, -1, 6] v.each do |iter| print "#{iter} " end print "\n"
"Right":
size_t frameViewsCount = frameViews.size(); for (size_t i = 0; i < frameViewsCount; ++i) frameViews[i]->updateLayoutAndStyleIfNeededRecursive();
"Right":
size_t frameViewsCount = frameViews.size(); for (size_t i = 0; i < frameViewsCount; ++i) frameViews[i]->updateLayoutAndStyleIfNeededRecursive();
BETTER:
for (auto& view : frameViews) { view.updateLayoutAndStyleIfNeededRecursive(); }
optional garbage collection
lambdas
modules
optionals
file system library
more coming soon