This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<typename T> | |
class Foo { | |
public: | |
Foo() { | |
++c; | |
} | |
private: | |
thread_local static int c; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
num test_enum | |
{ | |
A, | |
B, | |
C | |
}; | |
template<test_enum T> | |
int f(int x); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdlib> | |
#include <iostream> | |
struct Mammal { | |
Mammal() { std::cout << "Mammal::Mammal\n"; } | |
virtual ~Mammal() { std::cout << "Mammal::~Mammal\n"; }; | |
virtual void run() = 0; | |
virtual void walk() = 0; | |
virtual void move() { walk(); } | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int main() { | |
Mammal *m; | |
if (rand() % 2) { | |
m = new Cat(); | |
} else { | |
m = new Dog(); | |
} | |
m->walk(); | |
delete m; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Bat* bat = new Bat(); | |
Bird* b = bat; | |
Mammal* m = bat; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cstdlib> | |
struct Mammal { | |
Mammal() { std::cout << "Mammal::Mammal\n"; } | |
virtual ~Mammal() {} | |
virtual void walk() { std::cout << "Mammal::walk\n"; } | |
}; | |
struct Cat : Mammal { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct CloningLab { | |
subjects: Vec<Box<Mammal + Clone>>, | |
} | |
impl CloningLab { | |
fn clone_subjects(&self) -> Vec<Box<Mammal + Clone>> { | |
self.subjects.clone() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait CloneMammal: Clone + Mammal{} | |
impl<T> CloneMammal for T where T: Clone + Mammal{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let cat = Cat { | |
meow_factor: 7 | |
purr_factor: 8 | |
}; | |
// No problem, a CloneMammal is impl for Cat | |
let clone_mammal: &CloneMammal = cat; | |
// Error! | |
let clone: &Clone = &clone_mammal; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let cat = Cat { | |
meow_factor: 7 | |
purr_factor: 8 | |
}; | |
let dog = Dog { ... }; | |
let clone_mammal: &CloneMammal; | |
if get_random_bool() == true { | |
clone_mammal = &cat; |
OlderNewer