aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Kummerlaender2015-01-15 20:06:41 +0100
committerAdrian Kummerlaender2015-01-15 20:06:41 +0100
commitf63675d062156627dc23aa74d51356a7ab77c914 (patch)
treebb4e7923ee197d7172616356dd13d1d21496f505
parent5415ecfbdc5078826e09cf6525ff5c0289cedfd4 (diff)
downloadTypeAsValue-f63675d062156627dc23aa74d51356a7ab77c914.tar
TypeAsValue-f63675d062156627dc23aa74d51356a7ab77c914.tar.gz
TypeAsValue-f63675d062156627dc23aa74d51356a7ab77c914.tar.bz2
TypeAsValue-f63675d062156627dc23aa74d51356a7ab77c914.tar.lz
TypeAsValue-f63675d062156627dc23aa74d51356a7ab77c914.tar.xz
TypeAsValue-f63675d062156627dc23aa74d51356a7ab77c914.tar.zst
TypeAsValue-f63675d062156627dc23aa74d51356a7ab77c914.zip
Completed basic math operators and fixed their result type
* the result type now depends on the `decltype` of the performed operation
-rw-r--r--src/operation.h42
-rw-r--r--src/type.h9
-rw-r--r--test.cc6
3 files changed, 53 insertions, 4 deletions
diff --git a/src/operation.h b/src/operation.h
index c906b55..66dbc41 100644
--- a/src/operation.h
+++ b/src/operation.h
@@ -15,7 +15,10 @@ struct add {
"only values of the same type may be added"
);
- typedef Int<X::value + Y::value> type;
+ typedef std::integral_constant<
+ decltype(X::value + Y::value),
+ X::value + Y::value
+ > type;
};
template <
@@ -28,7 +31,42 @@ struct substract {
"only values of the same type may be substracted"
);
- typedef Int<X::value - Y::value> type;
+ typedef std::integral_constant<
+ decltype(X::value - Y::value),
+ X::value - Y::value
+ > type;
+};
+
+template <
+ typename X,
+ typename Y
+>
+struct multiply {
+ static_assert(
+ equal_type<X, Y>::value,
+ "only values of the same type may be multiplied"
+ );
+
+ typedef std::integral_constant<
+ decltype(X::value * Y::value),
+ X::value * Y::value
+ > type;
+};
+
+template <
+ typename X,
+ typename Y
+>
+struct divide {
+ static_assert(
+ equal_type<X, Y>::value,
+ "only values of the same type may be divided"
+ );
+
+ typedef std::integral_constant<
+ decltype(X::value / Y::value),
+ X::value / Y::value
+ > type;
};
}
diff --git a/src/type.h b/src/type.h
index 1149520..304bff4 100644
--- a/src/type.h
+++ b/src/type.h
@@ -20,6 +20,15 @@ using equal_type = typename std::integral_constant<
std::is_same<typename X::value_type, typename Y::value_type>::value
>::type;
+template <
+ typename X,
+ typename Y
+>
+using equal_value = typename std::integral_constant<
+ bool,
+ X::value == Y::value
+>::type;
+
}
#endif // TYPEASVALUE_SRC_TYPE_H_
diff --git a/test.cc b/test.cc
index 7f97762..06f0a17 100644
--- a/test.cc
+++ b/test.cc
@@ -10,8 +10,10 @@ TEST_F(TypeAsValueTest, Value) {
}
TEST_F(TypeAsValueTest, BasicMath) {
- EXPECT_EQ(3, ( tav::add<tav::Int<1>, tav::Int<2>>::type::value ));
- EXPECT_EQ(4, ( tav::substract<tav::Int<10>, tav::Int<6>>::type::value ));
+ EXPECT_EQ(3, ( tav::add<tav::Int<1>, tav::Int<2>>::type::value ));
+ EXPECT_EQ(4, ( tav::substract<tav::Int<10>, tav::Int<6>>::type::value ));
+ EXPECT_EQ(42, ( tav::multiply<tav::Int<2>, tav::Int<21>>::type::value ));
+ EXPECT_EQ(5, ( tav::divide<tav::Int<10>, tav::Int<2>>::type::value ));
}
int main(int argc, char **argv) {