stdcxx-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Martin Sebor <se...@roguewave.com>
Subject Re: Static assertions in tests?
Date Wed, 09 Jul 2008 23:36:13 GMT
Travis Vitek wrote:
>  
> 
> Martin Sebor wrote:
>> Eric Lemings wrote:
>>>  
>>> For compile-time tests, would it be preferable to use a 
>> static assertion
>>> or continue using good ol' rw_assert() even for compile-time 
>> checks?  In
>>> the former case, the test will fail to build and, in the latter case,
>>> the compile-time check will not be easily distinguisable from other
>>> runtime assertions.
>> I would recommend against using one component of the library
>> (static_assert) to test another.
>>
>> The approach taken by existing tests is to verify types and
>> signatures by using them in ways that would make the tests
>> ill-formed if they didn't match the requirements, causing
>> a compiler error. You can see examples of this approach in
>> the 23.vector.cons.cpp test that was just mentioned.
>>
> 
> I happen to use this trick ..
> 
>   typedef char assert_0 [(cond) ? 1 : -1];

I think we're talking about two different things. I assumed
Brad was asking about tests to check things like the expected
signatures of functions, such as the signature of std::get()
below:

     std::tuple<int> t;
     std::get<0>(t);

My approach to writing a test for this would be along these
lines:

#include <cassert>
#include <cstddef>
#include <tuple>

template <std::size_t N, class Tuple, class ExpectSig>
void check_get_signature ()
{
     ExpectSig* const sig = &std::get<N, Tuple>;
     _RWSTD_UNUSED (sig);
}

void test_get_signatures ()
{
#define TEST(N, Tuple, RetT) \
     check_get_signature<N, Tuple, RetT (const std::tuple<int>&)>()

     TEST (0, std::tuple<int>, const int&);
     TEST (1, std::tuple<int, char*>, char*&);
     ...
}

Martin

Mime
View raw message