Navigation Menu

Skip to content

Commit

Permalink
allow stronger typing
Browse files Browse the repository at this point in the history
  • Loading branch information
Speakus committed Oct 3, 2014
1 parent db806f9 commit a360482
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
8 changes: 6 additions & 2 deletions Primitive.hpp
Expand Up @@ -11,8 +11,12 @@ _Pragma("once");
#define OPERATOR_FUNC_0ARG(res, oper) \
res operator oper(void) const { return oper(value); }

template <class T> class Primitive {
typedef Primitive<T> X;
// you could use N from int (or enum) if you want stronger typing between different types
// example:
// typedef Primitive<double, 1> timeT;
// typedef Primitive<double, 2> volumeLevelT;
template <class T, int N = 0> class Primitive {
typedef Primitive<T, N> X;
private:
T value;
public:
Expand Down
12 changes: 12 additions & 0 deletions test_Primitive.cpp
Expand Up @@ -26,10 +26,22 @@ static void test_Primitive(void) {
assert(1.0f == valf);
}

static void test_compilation_fails(void) {
Primitive<int> tmp1 = 1;
assert(1 == tmp1);
Primitive<int, 1> tmp2 = 2;
assert(2 == tmp2);
#if 0 // uncomment it for test compilation fails
int i = tmp1 + tmp2;
assert(3 == i);
#endif
}

#include <stdio.h> // for printf()

int main(void) {
test_Primitive();
test_compilation_fails();
printf("test passed\n");
return 0;
}

0 comments on commit a360482

Please sign in to comment.