This file contains hidden or 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 x = 2; | |
int y = 9; | |
int z = 4; | |
// f captures nothing | |
std::function<int(int)> f = [](int i) { | |
return i + 1; | |
}; | |
// g captures x, y and z |
This file contains hidden or 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 Functor { | |
// The context, or capture | |
// For example, an int and an unsigned | |
int i; | |
unsigned N; | |
// The lambda | |
int operator() (int j) const { | |
// For example, a small math function | |
return i * j + N; |
This file contains hidden or 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 SomeClassWithAReallyLongName { | |
// ... | |
}; | |
SomeClassWithAReallyLongName foo() { | |
SomeClassWithAReallyLongName x; | |
return x; | |
} | |
int main() { |
This file contains hidden or 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
Vector<1> v; | |
Vector<2> v; | |
Vector<3> u; | |
// etc... |
This file contains hidden or 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<int D> | |
struct Vector { | |
static constexpr unsigned N = D; | |
int data[N]; | |
Vector(int fill = 0) { | |
for (int i = 0; i < N; ++i) { | |
data[i] = fill; | |
} |
This file contains hidden or 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
public final class Vector3 { | |
public final float x; | |
public final float y; | |
public final float z; | |
public Vector3(float x, float y, float z) { | |
this.x = x; | |
this.y = y; | |
this.z = z; |
This file contains hidden or 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
Vector2 v = { 1, 2 }; | |
Vector2 u = { 3, 4 }; | |
Vector2 w = v + w; // Much better! |
This file contains hidden or 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 Vector2 { | |
float x; | |
float y; | |
}; | |
inline Vector2 operator+(Vector2 const& lhs, Vector2 const& rhs) { | |
return { lhs.x + rhs.x, lhs.y + lhs.y }; | |
} |
This file contains hidden or 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
Vector2 v = new Vector2(1, 2); | |
Vector2 u = new Vector2(3, 4); | |
Vector2 w = v.add(w); |
This file contains hidden or 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
public final class Vector2 { | |
public final float x; | |
public final float y; | |
public Vector2(float x, float y) { | |
this.x = x; | |
this.y = y; | |
} | |