stdcxx-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Eric Lemings" <Eric.Lemi...@roguewave.com>
Subject RE: static_assert config check
Date Thu, 12 Jun 2008 23:05:49 GMT
 

> -----Original Message-----
> From: Eric Lemings [mailto:Eric.Lemings@roguewave.com] 
> Sent: Thursday, June 12, 2008 4:37 PM
> To: dev@stdcxx.apache.org
> Subject: RE: static_assert config check
> 
>  
> 
> > -----Original Message-----
> > From: Travis Vitek [mailto:Travis.Vitek@roguewave.com] 
> > Sent: Thursday, June 12, 2008 4:00 PM
> > To: dev@stdcxx.apache.org
> > Subject: RE: static_assert config check
> > 
> >  
> > 
> > Martin Sebor wrote:
> > >Travis Vitek wrote:
> > >>> Eric Lemings wrote:
> > >>>
> > >>>
> > >>> How's this for a suitable configuration check for static_assert?
> > >>>
> > >>> 	// compile-only test
> > >>>
> > >>> 	static_assert (sizeof (char) == 1, "impossible");
> > >>>
> > >>> 	template <int I> struct S {
> > >>> 	    static_assert (I >= 0, "template parameter I must be
> > >>> non-negative");
> > >>> 	};
> > >>>
> > >> 
> > >> I've written an errily similar test already (pasted below)
> > >> 
> > >> I think you should probably instantiate S somewhere and it 
> > might be a
> > >> good idea put a line break before 'struct' so that your code 
> > >> conforms to our 'coding standards'. 
> > >[...]
> > >
> > >It's probably overkill, but just as an FYI, to verify this works
> > >both ways the test would need to be negative, i.e., named NO_XXX,
> > >and write #define _RWSTD_NO_XXX to stdout if the negative assert
> > >failed to fire.
> > 
> > So how exactly is the test supposed to write anything to 
> stdout if it
> > doesn't compile? If the expression of the static_assert is 
> false, the
> > program is ill-formed and the compiler is to emit a diagnostic.
> > 
> > I'm looking at this and if I name the test 
> NO_STATIC_ASSERT.cpp and it
> > fails to compile, the macro _RWSTD_NO_STATIC_ASSERT wll be 
> defined, so
> > using the NO_ prefix doesn't really buy me anything. I 
> don't think it
> > would be right to make it so that if a NO_XXX test fails to 
> > compile the
> > macro _RWSTD_NO_XXX will not be defined.
> > 
> > The only way I see to ensure that static_assert is actually 
> > working both
> > ways is to write two tests, one testing for passing conditions
> > [STATIC_ASSERT_PASS.cpp], and the other testing for failing 
> conditions
> > [STATIC_ASSERT_FAIL.cpp]. Then we would define 
> _RWSTD_NO_STATIC_ASSERT
> > like so...
> > 
> >   #if    !defined (_RWSTD_NO_STATIC_ASSERT_PASS)
> >       ||  defined (_RWSTD_NO_STATIC_ASSERT_FAIL)
> >   // STATIC_ASSERT_PASS.cpp failed to compile
> >   // STATIC_ASSERT_FAIL.cpp compiled without error
> >   #  define _RWSTD_NO_STATIC_ASSERT
> >   #endif
> > 
> > Is that overkill?
> 
> Or...you could run the negative test _first_ and if that fails (i.e.
> does not fire as assertion), run the positive test which will produce
> _RWSTD_NO_STATIC_ASSERT only if one or both tests failed.

Ehh, to clarify a bit:

STATIC_ASSERT_FAIL.cpp:
	static_assert (false, "Grep for this exact text in the compiler
diagnostics");

STATIC_ASSSERT.cpp:
	(Same as Travis' static assert test posted earlier.)

In pseudo-script:

compile the negative test
if the compile command succeeds,
    {echo "#define _RWSTD_NO_STATIC_ASSERT"
>>$BUILDDIR/include/config.h}
else
    grep the compiler diagnostic output for the text in the
static_assert statement
    if the text is found in the compiler output,
        compile the STATIC_ASSERT.cpp
        if the compile command fails,
            {echo "#define _RWSTD_NO_STATIC_ASSERT"
>>$BUILDDIR/include/config.h}
        endif
    endif
endif

I think I got that right. :P

Brad.

Mime
View raw message