do some whitespace janitor work
This commit is contained in:
parent
0e43b30736
commit
7db74eb991
13 changed files with 49 additions and 55 deletions
|
@ -14,7 +14,7 @@ struct myclass {
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
int init = 100;
|
int init = 100;
|
||||||
int numbers[] = {10,20,30};
|
int numbers[] = {10, 20, 30};
|
||||||
int res;
|
int res;
|
||||||
|
|
||||||
std::cout << "using default accumulate: ";
|
std::cout << "using default accumulate: ";
|
||||||
|
|
|
@ -6,12 +6,12 @@ void classic() {
|
||||||
int array[2];
|
int array[2];
|
||||||
array[0] = 1;
|
array[0] = 1;
|
||||||
array[1] = 2;
|
array[1] = 2;
|
||||||
array[3] = 3; // <- clang++ -Warray-bounds warns about this
|
array[3] = 3; // <- clang++ -Warray-bounds warns about this
|
||||||
|
|
||||||
int n = 10;
|
int n = 10;
|
||||||
std::cout << array[3] << std::endl; // no warning!
|
std::cout << array[3] << std::endl; // no warning!
|
||||||
std::cout << array[4] << std::endl; // no warning!
|
std::cout << array[4] << std::endl; // no warning!
|
||||||
std::cout << array[n-1] << std::endl; // no warning!
|
std::cout << array[n-1] << std::endl; // no warning!
|
||||||
}
|
}
|
||||||
|
|
||||||
void std_array() {
|
void std_array() {
|
||||||
|
@ -22,7 +22,7 @@ void std_array() {
|
||||||
// whoopsie
|
// whoopsie
|
||||||
array[2] = 3;
|
array[2] = 3;
|
||||||
|
|
||||||
for(auto &e: array) {
|
for (auto &e : array) {
|
||||||
std::cout << e << " ";
|
std::cout << e << " ";
|
||||||
}
|
}
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
@ -44,10 +44,10 @@ void std_vector() {
|
||||||
}
|
}
|
||||||
vector.resize(10);
|
vector.resize(10);
|
||||||
vector.at(2) = 3;
|
vector.at(2) = 3;
|
||||||
vector[1] = 2; // should now work
|
vector[1] = 2; // should now work
|
||||||
// vector[3] = 3; // SEGFAULT
|
// vector[3] = 3; // SEGFAULT
|
||||||
|
|
||||||
for(auto &e: vector) {
|
for (auto &e : vector) {
|
||||||
std::cout << e << " ";
|
std::cout << e << " ";
|
||||||
}
|
}
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
|
|
@ -13,9 +13,9 @@ int main() {
|
||||||
|
|
||||||
y = x;
|
y = x;
|
||||||
|
|
||||||
std::cout << x.get() << std::endl; // Print NULL
|
std::cout << x.get() << std::endl; // Print NULL
|
||||||
assert(x.get() == NULL);
|
assert(x.get() == NULL);
|
||||||
std::cout << y.get() << std::endl; // Print non-NULL address i
|
std::cout << y.get() << std::endl; // Print non-NULL address i
|
||||||
assert(y.get() != NULL);
|
assert(y.get() != NULL);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
13
casts.cpp
13
casts.cpp
|
@ -3,11 +3,9 @@
|
||||||
|
|
||||||
class mybase {
|
class mybase {
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
std::string ids;
|
std::string ids;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
mybase(std::string ids)
|
mybase(std::string ids)
|
||||||
: ids(ids) {}
|
: ids(ids) {}
|
||||||
|
|
||||||
|
@ -17,16 +15,13 @@ class mybase {
|
||||||
};
|
};
|
||||||
|
|
||||||
class myclass : public mybase {
|
class myclass : public mybase {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
myclass(std::string ids)
|
myclass(std::string ids)
|
||||||
: mybase(ids) {}
|
: mybase(ids) {}
|
||||||
|
|
||||||
void foo() {
|
void foo() {
|
||||||
std::cout << "i'm a myclass! ids: " << ids << std::endl;
|
std::cout << "i'm a myclass! ids: " << ids << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
void nocast(myclass* x) {
|
void nocast(myclass* x) {
|
||||||
|
@ -40,8 +35,8 @@ void ccast(void* x) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void staticcast(void* x) {
|
void staticcast(void* x) {
|
||||||
// C++ static_cast. When I *know* it's myclass*, and want to revert an implicit
|
// C++ static_cast. When I *know* it's myclass*, and want to revert an
|
||||||
// conversion.
|
// implicit conversion.
|
||||||
myclass* foo = static_cast<myclass*>(x);
|
myclass* foo = static_cast<myclass*>(x);
|
||||||
foo->foo();
|
foo->foo();
|
||||||
}
|
}
|
||||||
|
@ -64,11 +59,11 @@ int main() {
|
||||||
|
|
||||||
std::cout << "== ccast" << std::endl;
|
std::cout << "== ccast" << std::endl;
|
||||||
ccast(&a);
|
ccast(&a);
|
||||||
ccast(&base); // XXX actually works!
|
ccast(&base); // XXX actually works!
|
||||||
|
|
||||||
std::cout << "== staticcast" << std::endl;
|
std::cout << "== staticcast" << std::endl;
|
||||||
staticcast(&a);
|
staticcast(&a);
|
||||||
staticcast(&base); // XXX actually works!
|
staticcast(&base); // XXX actually works!
|
||||||
|
|
||||||
std::cout << "== dynamiccast" << std::endl;
|
std::cout << "== dynamiccast" << std::endl;
|
||||||
dynamiccast(&a);
|
dynamiccast(&a);
|
||||||
|
|
26
classes.cpp
26
classes.cpp
|
@ -63,37 +63,37 @@ int main() {
|
||||||
callptr(&animal);
|
callptr(&animal);
|
||||||
|
|
||||||
std::cout << "== cat" << std::endl;
|
std::cout << "== cat" << std::endl;
|
||||||
animal = cat; // <- lol, operator
|
animal = cat; // <- lol, operator
|
||||||
animal.makeSound();
|
animal.makeSound();
|
||||||
call(cat);
|
call(cat);
|
||||||
callref(cat); // <- meow
|
callref(cat); // <- meow
|
||||||
callref(animal); // <- generic
|
callref(animal); // <- generic
|
||||||
callptr(&cat); // <- meow
|
callptr(&cat); // <- meow
|
||||||
|
|
||||||
std::cout << "== cow" << std::endl;
|
std::cout << "== cow" << std::endl;
|
||||||
animal = cow; // <- lol, operator
|
animal = cow; // <- lol, operator
|
||||||
animal.makeSound();
|
animal.makeSound();
|
||||||
call(cow);
|
call(cow);
|
||||||
callref(cow); // <- mooh
|
callref(cow); // <- mooh
|
||||||
callptr(&cow); // <- mooh
|
callptr(&cow); // <- mooh
|
||||||
|
|
||||||
std::cout << "== refs" << std::endl;
|
std::cout << "== refs" << std::endl;
|
||||||
Animal &aref = cat;
|
Animal &aref = cat;
|
||||||
aref.makeSound(); // <- meow
|
aref.makeSound(); // <- meow
|
||||||
aref = cow; // <- lol, operator
|
aref = cow; // <- lol, operator
|
||||||
aref.makeSound(); // <- meow, still
|
aref.makeSound(); // <- meow, still
|
||||||
|
|
||||||
std::cout << "== vector<Animal>:" << std::endl;
|
std::cout << "== vector<Animal>:" << std::endl;
|
||||||
// Does nothing:
|
// Does nothing:
|
||||||
std::vector<Animal> animals = { cat, cow }; // <- Copies
|
std::vector<Animal> animals = { cat, cow }; // <- Copies
|
||||||
for (auto &a: animals) {
|
for (auto &a : animals) {
|
||||||
a.makeSound();
|
a.makeSound();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "== vector<Animal*>:" << std::endl;
|
std::cout << "== vector<Animal*>:" << std::endl;
|
||||||
// Meow? Mooh.
|
// Meow? Mooh.
|
||||||
std::vector<Animal*> animalptrs = { &cat, &cow };
|
std::vector<Animal*> animalptrs = { &cat, &cow };
|
||||||
for (auto &a: animalptrs) {
|
for (auto &a : animalptrs) {
|
||||||
a->makeSound();
|
a->makeSound();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,8 +6,8 @@
|
||||||
#include <chrono> // std::chrono::milliseconds
|
#include <chrono> // std::chrono::milliseconds
|
||||||
|
|
||||||
// a non-optimized way of checking for prime numbers.
|
// a non-optimized way of checking for prime numbers.
|
||||||
bool is_prime (long int x) {
|
bool is_prime(long int x) {
|
||||||
for (long int i=2; i<x; ++i) {
|
for (long int i = 2; i < x; ++i) {
|
||||||
if (x%i == 0) return false;
|
if (x%i == 0) return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
@ -35,12 +35,13 @@ void implicitly_waiting() {
|
||||||
long int p2 = 685102597328182763;
|
long int p2 = 685102597328182763;
|
||||||
|
|
||||||
std::future<bool> fut = std::async(std::launch::async, is_prime, p2);
|
std::future<bool> fut = std::async(std::launch::async, is_prime, p2);
|
||||||
std::cout << "just getting the result, doing an implicit wait():" << std::endl;
|
std::cout << "just getting the result, doing an implicit wait():";
|
||||||
|
std::cout << std::endl;
|
||||||
bool x = fut.get();
|
bool x = fut.get();
|
||||||
std::cout << p2 << " " << (x?"is":"is not") << " prime." << std::endl;
|
std::cout << p2 << " " << (x?"is":"is not") << " prime." << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main () {
|
int main() {
|
||||||
explicitly_waiting();
|
explicitly_waiting();
|
||||||
implicitly_waiting();
|
implicitly_waiting();
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -7,7 +7,6 @@
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|
||||||
// notice how the lists are nested to match the templates' parameters:
|
// notice how the lists are nested to match the templates' parameters:
|
||||||
map<string, vector<pair<string, int>>> name_languages_year {
|
map<string, vector<pair<string, int>>> name_languages_year {
|
||||||
{"Dennis Ritchie", {{"B", 1969}, {"C", 1973}}},
|
{"Dennis Ritchie", {{"B", 1969}, {"C", 1973}}},
|
||||||
|
@ -27,5 +26,4 @@ int main() {
|
||||||
|
|
||||||
// prints 'Lisp':
|
// prints 'Lisp':
|
||||||
cout << name_languages_year["John McCarthy"].at(0).first << endl;
|
cout << name_languages_year["John McCarthy"].at(0).first << endl;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
|
||||||
struct A {
|
struct A {
|
||||||
A() { puts("A()"); }
|
A() { puts("A()"); }
|
||||||
~A() { puts("~A()"); }
|
~A() { puts("~A()"); }
|
||||||
};
|
};
|
||||||
|
|
||||||
struct B {
|
struct B {
|
||||||
B() { puts("B()"); }
|
B() { puts("B()"); }
|
||||||
~B() { puts("~B()"); }
|
~B() { puts("~B()"); }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
/* http://en.wikipedia.org/wiki/C++14 */
|
// http://en.wikipedia.org/wiki/C++14
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
5
rtti.cpp
5
rtti.cpp
|
@ -32,7 +32,8 @@ class Hammer : public Tool {
|
||||||
}
|
}
|
||||||
|
|
||||||
void use(Nail nail) {
|
void use(Nail nail) {
|
||||||
std::cout << "The nail is " << nail.getLength() << " cm long" << std::endl;
|
std::cout << "The nail is " << nail.getLength() << " cm long"
|
||||||
|
<< std::endl;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -50,7 +51,7 @@ void useSomeTool(Tool &tool) {
|
||||||
std::cout << "Look, it's a " << typeid(tool).name() << "!" << std::endl;
|
std::cout << "Look, it's a " << typeid(tool).name() << "!" << std::endl;
|
||||||
|
|
||||||
// XXX What about subclasses of Hammer?
|
// XXX What about subclasses of Hammer?
|
||||||
if(typeid(tool) == typeid(Hammer)) {
|
if (typeid(tool) == typeid(Hammer)) {
|
||||||
std::cout << "Stop! ";
|
std::cout << "Stop! ";
|
||||||
}
|
}
|
||||||
tool.use();
|
tool.use();
|
||||||
|
|
|
@ -9,7 +9,7 @@ void shared_ptr() {
|
||||||
std::cout << __FUNCTION__ << std::endl;
|
std::cout << __FUNCTION__ << std::endl;
|
||||||
|
|
||||||
std::shared_ptr<int> p1(new int(5));
|
std::shared_ptr<int> p1(new int(5));
|
||||||
std::shared_ptr<int> p2 = p1; // Both now own the memory.
|
std::shared_ptr<int> p2 = p1; // Both now own the memory.
|
||||||
assert(p1.use_count() == 2);
|
assert(p1.use_count() == 2);
|
||||||
|
|
||||||
// Memory still exists, due to p2:
|
// Memory still exists, due to p2:
|
||||||
|
@ -29,7 +29,7 @@ void shared_ptr_container() {
|
||||||
std::cout << __FUNCTION__ << std::endl;
|
std::cout << __FUNCTION__ << std::endl;
|
||||||
|
|
||||||
std::shared_ptr<int> p1(new int(5));
|
std::shared_ptr<int> p1(new int(5));
|
||||||
std::shared_ptr<int> p2 = p1; // Both now own the memory.
|
std::shared_ptr<int> p2 = p1; // Both now own the memory.
|
||||||
assert(p1.use_count() == 2);
|
assert(p1.use_count() == 2);
|
||||||
|
|
||||||
std::vector<std::shared_ptr<int>> v;
|
std::vector<std::shared_ptr<int>> v;
|
||||||
|
@ -49,20 +49,20 @@ void weak_ptr() {
|
||||||
std::cout << __FUNCTION__ << std::endl;
|
std::cout << __FUNCTION__ << std::endl;
|
||||||
|
|
||||||
std::shared_ptr<int> p1(new int(5));
|
std::shared_ptr<int> p1(new int(5));
|
||||||
std::weak_ptr<int> wp1 = p1; // p1 owns the memory.
|
std::weak_ptr<int> wp1 = p1; // p1 owns the memory.
|
||||||
|
|
||||||
{
|
{
|
||||||
std::shared_ptr<int> p2 = wp1.lock(); //Now p1 and p2 own the memory.
|
std::shared_ptr<int> p2 = wp1.lock(); // Now p1 and p2 own the memory.
|
||||||
assert(p2 != NULL);
|
assert(p2 != NULL);
|
||||||
if (p2) { // As p2 is initialized from a weak pointer,
|
if (p2) { // As p2 is initialized from a weak pointer,
|
||||||
// you have to check if the memory still exists!
|
// you have to check if the memory still exists!
|
||||||
// Do something with p2
|
// Do something with p2
|
||||||
}
|
}
|
||||||
} // p2 is destroyed. Memory is owned by p1.
|
} // p2 is destroyed. Memory is owned by p1.
|
||||||
|
|
||||||
assert(wp1.lock() != NULL);
|
assert(wp1.lock() != NULL);
|
||||||
assert(wp1.use_count() == 1);
|
assert(wp1.use_count() == 1);
|
||||||
p1.reset(); // Memory is deleted.
|
p1.reset(); // Memory is deleted.
|
||||||
assert(wp1.lock() == NULL);
|
assert(wp1.lock() == NULL);
|
||||||
assert(wp1.use_count() == 0);
|
assert(wp1.use_count() == 0);
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
int main()
|
int main() {
|
||||||
{
|
|
||||||
int someInteger = 256;
|
int someInteger = 256;
|
||||||
short someShort;
|
short someShort;
|
||||||
long someLong;
|
long someLong;
|
||||||
|
@ -16,7 +15,7 @@ int main()
|
||||||
assert(someInteger == 514);
|
assert(someInteger == 514);
|
||||||
|
|
||||||
someShort = (short) someInteger;
|
someShort = (short) someInteger;
|
||||||
assert((int) someShort == 514); // (short more than 1 byte)
|
assert((int) someShort == 514); // (short more than 1 byte)
|
||||||
|
|
||||||
someLong = someShort * 10000;
|
someLong = someShort * 10000;
|
||||||
assert(someLong == 5140000l);
|
assert(someLong == 5140000l);
|
||||||
|
|
|
@ -26,7 +26,7 @@ void container() {
|
||||||
v.push_back(std::move(q));
|
v.push_back(std::move(q));
|
||||||
assert(q == NULL);
|
assert(q == NULL);
|
||||||
|
|
||||||
for(auto &e: v) {
|
for (auto &e : v) {
|
||||||
std::cout << *e << std::endl;
|
std::cout << *e << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue