rubyish



rubyish

0 0


rubyish

Rubyish C++

On Github jwfearn / rubyish

Rubyish C++11

John Fearnside

john@moz.com

github.com/jwfearn/rubyish

C++ isn't much like Ruby...

and never will be...

but it's getting a little closer

History

C++98 First ISO standard

C++03 Bug fix

C++11 First major revision (13 years later)

C++11

Approved 12 August 2011

Many features have been available in major compilers for a while

C++11 Available Now

  • Apple LLVM 5.0
  • clang 3.3
  • gcc 4.8.1 (mostly)
  • Visual C++ 2013

What's new

  • Language features
  • Library features
  • Usability features

Adoption

Existing code still works

Compiler support relatively new

New programmers leary

Not your father's C++

Old guidance doesn't necessarily apply

Blink example

“Prefer index over iterator in Vector iterations for a terse, easier-to-read code.” Right:

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();

-- Blink style guide

we'll come back to this

C++ can be simpler, like Ruby

Dynamic Arrays

Array in Ruby

std::vector in C++

Array Example in Ruby

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

Vector Example in C++98

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

Vector Example in C++98

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.

Vector Example in C++98

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

Vector Example in C++11

v1.cpp

#include <iostream>
#include <vector>

int main() {
  //...
  std::cout << std::endl;
  return 0;
}

Initialization Lists

Uniform Initialization

Vector Example in C++11

C++98:

int arr[] = {-99, 0, 42, 3, -1, 6};
std::vector<int> v(arr, arr + sizeof(arr) / sizeof(int));

Vector Example in C++11

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

Vector Example in C++11

C++98:

std::vector<int>::const_iterator iter = v.begin();

Vector Example in C++11

C++98:

std::vector<int>::const_iterator iter = v.begin();

C++11:

auto iter = v.begin();

Range-based for

Vector Example in C++11

C++98:

std::vector<int>::const_iterator end = v.end();
std::vector<int>::const_iterator iter = v.begin();
for (; iter != end; ++iter) {

Vector Example in C++11

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) {

Vector Example in C++11

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

Vector Example in C++11

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.

Vector Example in C++11

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

Rubyish?

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"

Blink example (reprise)

"Right":

size_t frameViewsCount = frameViews.size();
for (size_t i = 0; i < frameViewsCount; ++i)
  frameViews[i]->updateLayoutAndStyleIfNeededRecursive();

Blink example (reprise)

"Right":

size_t frameViewsCount = frameViews.size();
for (size_t i = 0; i < frameViewsCount; ++i)
  frameViews[i]->updateLayoutAndStyleIfNeededRecursive();

BETTER:

for (auto& view : frameViews) {
  view.updateLayoutAndStyleIfNeededRecursive();
}

Other Rubyish features

optional garbage collection

lambdas

Coming soon (C++14, C++17)

modules

optionals

file system library

Resources & Books

more coming soon