Created
December 30, 2012 22:44
-
-
Save lcapaldo/4415772 to your computer and use it in GitHub Desktop.
(Pseudo-) Path Dependent types, C++11 style.
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 <set> | |
#include <assert.h> | |
template<typename Unutterable> | |
struct Board | |
{ | |
int length; | |
int height; | |
struct Coordinate | |
{ | |
int x; | |
int y; | |
private: | |
Coordinate(int x_, int y_) : x(x_), y(y_) {} | |
friend class Board; | |
public: | |
Coordinate() = default; | |
Coordinate(const Coordinate&) = default; | |
Coordinate(Coordinate&&) = default; | |
bool operator==(const Coordinate& o) const { return x == o.x && y == o.y; } | |
bool operator <(const Coordinate& o) const { return x < o.x || (x == o.x && y < o.y); } | |
}; | |
std::set<Coordinate> occupied; | |
Board(int l, int h) : length(l), height(h) {} | |
Coordinate newCoordinate(int x, int y) | |
{ | |
assert(0 <= x && x <= length && 0 <= y && y <= height); | |
return Coordinate(x,y); | |
} | |
}; | |
template<typename U> | |
Board<U> do_make_board(U u, int l, int w) | |
{ | |
return Board<U>(l, w); | |
} | |
#define make_board(l,w) (do_make_board([](){}, l, w)) | |
int main() | |
{ | |
auto b1 = make_board(20, 20); | |
auto b2 = make_board(30, 30); | |
auto c1 = b1.newCoordinate(15, 15); | |
auto c2 = b2.newCoordinate(25,25); | |
b1.occupied.insert(c1); | |
//b1.occupied.insert(c2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment