Reputation: 2196
I have a struct that looks something like this:
struct Foo {
typedef size_t result_type;
template <class TYPE>
result_type operator()(const TYPE& type) const;
};
I'm testing this struct and I want to be able to assert that the return type of the operator()
is equal to size_t
. If I could used C++11, I would go to decltype
, however, I only have access to C++03. There are a few reasons I want to test something so seemingly obvious:
What options do I have to do this in C++03? I don't care if it is compile time or run time checking, just something that allows me to do an assert in my test driver.
Upvotes: 1
Views: 144
Reputation: 5871
If you need to enforce binary compatibility of some data structures or messages, you may want to check the properties of the type rather than the type itself. These may change for what is called size_t in two different architectures.
assert(EXPECTED_SIZE == sizeof(result_type));
assert(EXPECTED_MIN == std::numeric_limits<result_type>::min());
assert(EXPECTED_MAX == std::numeric_limits<result_type>::max());
Upvotes: 0
Reputation: 106609
C++03 does not have a way of directly asking for the return type. You can deduce it with a function template (as Cody Gray suggests in comments) but for your testing purposes you don't need to do that. Just call your function under test and hand the result to an overload set like this:
template <typename AnythingElse>
void Foo(AnythingElse const&) { assert(false); }
void Foo(YourType const&) { /* pass */ }
That said, I would say testing the return type is probably a mistake -- testing an implementation detail instead of testing results.
Upvotes: 4