You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@stdcxx.apache.org by Eric Lemings <Er...@roguewave.com> on 2008/06/11 20:33:53 UTC
static_assert config check
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");
};
Brad.
Re: static_assert config check
Posted by Eric Lemings <er...@roguewave.com>.
On Jun 11, 2008, at 1:57 PM, Travis Vitek wrote:
>
>
> Eric Lemings wrote:
>>
>>
>>> Travis Vitek wrote:
>>>
>>>
>>>
>> ...
>>>>
>>>
>>> I've written an errily similar test already (pasted below)
>>>
>>
>> I like your test better except for two things.
>>
>> 1. Need a static assert at file scope similar to the one in main().
>>
>> 2. Change main() to foo(). We don't need (nor want) to run the a
>> program; just compile the source file (which is when static_assert's
>> are supposed to fire).
>>
>
> True on both counts.
>
> On a related note, we already have __rw_compile_assert [see rw/
> _defs.h],
> but it doesn't work at global or class scope.
>
> I provided an __rw_static_assert for use with type_traits in my
> original
> patch, but it didn't work outside of a template. I've got an updated
> version that will work in all cases. I'm thinking that I should
> replace
> the existing __rw_compile_assert with __rw_static_assert, and then
> define a macro _RWSTD_STATIC_ASSERT(Cond,Mesg) that uses static_assert
> if it exists, or falls back to ours as needed.
And the macro can be used the same in all cases regardless of whether
it is implemented using the compiler's static_assert or our own static
assert?
Brad.
RE: static_assert config check
Posted by Travis Vitek <Tr...@roguewave.com>.
Eric Lemings wrote:
>
>
>> Travis Vitek wrote:
>>
>>
>>
>...
>> >
>>
>> I've written an errily similar test already (pasted below)
>>
>
>I like your test better except for two things.
>
>1. Need a static assert at file scope similar to the one in main().
>
>2. Change main() to foo(). We don't need (nor want) to run the a
>program; just compile the source file (which is when static_assert's
>are supposed to fire).
>
True on both counts.
On a related note, we already have __rw_compile_assert [see rw/_defs.h],
but it doesn't work at global or class scope.
I provided an __rw_static_assert for use with type_traits in my original
patch, but it didn't work outside of a template. I've got an updated
version that will work in all cases. I'm thinking that I should replace
the existing __rw_compile_assert with __rw_static_assert, and then
define a macro _RWSTD_STATIC_ASSERT(Cond,Mesg) that uses static_assert
if it exists, or falls back to ours as needed.
>Brad.
>
RE: static_assert config check
Posted by Eric Lemings <Er...@roguewave.com>.
> -----Original Message-----
> From: Travis Vitek [mailto:Travis.Vitek@roguewave.com]
> Sent: Wednesday, June 11, 2008 1:08 PM
> To: dev@stdcxx.apache.org
> Subject: RE: static_assert config check
>
>
>
...
> >
>
> I've written an errily similar test already (pasted below)
>
I like your test better except for two things.
1. Need a static assert at file scope similar to the one in main().
2. Change main() to foo(). We don't need (nor want) to run the a
program; just compile the source file (which is when static_assert's
are supposed to fire).
Brad.
Re: static_assert config check
Posted by Martin Sebor <se...@roguewave.com>.
Eric Lemings wrote:
>
>
>> -----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.
Something like that. I was mainly aiming to point out that
we have both positive and negative tests. The static_assert
test clearly wasn't a good example because, as Travis noted,
it would need to be split up into two.
Martin
RE: static_assert config check
Posted by Eric Lemings <Er...@roguewave.com>.
> -----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.
RE: static_assert config check
Posted by Eric Lemings <Er...@roguewave.com>.
> -----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.
Brad.
RE: static_assert config check
Posted by Travis Vitek <Tr...@roguewave.com>.
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?
>Martin
>
>> template <int _N>
>> struct S
>> {
>> static_assert (0 < _N, "fail");
>> };
>>
>> template <int _N>
>> void f ()
>> {
>> static_assert (0 < _N, "fail");
>> }
>>
>> int main ()
>> {
>> S<1> s1;
>> f<1>();
>> static_assert (1, "pass");
>>
>> return 0;
>> }
>
>
Re: static_assert config check
Posted by Martin Sebor <se...@roguewave.com>.
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.
Martin
> template <int _N>
> struct S
> {
> static_assert (0 < _N, "fail");
> };
>
> template <int _N>
> void f ()
> {
> static_assert (0 < _N, "fail");
> }
>
> int main ()
> {
> S<1> s1;
> f<1>();
> static_assert (1, "pass");
>
> return 0;
> }
RE: static_assert config check
Posted by Travis Vitek <Tr...@roguewave.com>.
>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'.
>Brad.
>
// checking for static_assert
/***********************************************************************
****
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the License); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 2008 Rogue Wave Software, Inc.
*
************************************************************************
**/
template <int _N>
struct S
{
static_assert (0 < _N, "fail");
};
template <int _N>
void f ()
{
static_assert (0 < _N, "fail");
}
int main ()
{
S<1> s1;
f<1>();
static_assert (1, "pass");
return 0;
}