Topic: Proposal: about arithmetic constant expr


Author: kuha@research.nokia.com (Kuha Jorma NRC/Hki)
Date: 1996/08/09
Raw View
According to Draft chapt. 5.19, this is a valid C++-program, the behaviour
of
which is undefined:

enum {Test = 1/0};

int main()
{
    return Test;
}

as the effect of division by zero is undefined.

Proposal: Division by zero in constant expression should not be allowed,
resulting in compile error instead of undefined run-time behaviour.

Motivation: This would allow us for example to check
restrictions on template-arguments on compile-time like this:

template<int i, int j>  class A
//
// "i>= j" should always hold
{
    enum {assertion1 = 1 / (i >= j) };
    enum {assertion2 = (i >= j) ? 1 : 1/0 };  // would this be OK too?
};

A <3,2> a1; // fine
A <2,3> a2; // would not compile

 --
Jorma.Kuha@research.nokia.com




[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Chelly Green <chelly@eden.com>
Date: 1996/08/09
Raw View
Kuha Jorma NRC/Hki wrote:
>
> According to Draft chapt. 5.19, this is a valid C++-program, the behaviour
> of which is undefined:
>
> enum {Test = 1/0};
>
> int main()
> {
>     return Test;
> }
>
> as the effect of division by zero is undefined.
>
> Proposal: Division by zero in constant expression should not be allowed,
> resulting in compile error instead of undefined run-time behaviour.
>
> Motivation: This would allow us for example to check
> restrictions on template-arguments on compile-time like this:

Sure seems like an indirect way to accomplish it...

> template<int i, int j>  class A
> //
> // "i>= j" should always hold
> {
>     enum {assertion1 = 1 / (i >= j) };
>     enum {assertion2 = (i >= j) ? 1 : 1/0 };  // would this be OK too?
> };

If it is an error, who says the compiler doesn't just give the error
when
it first sees the template class?

Couldn't you just have a const_assert macro?

    template<int i> class const_assert_class { public: enum { i }; };
    class const_assert_class<0> { };

    #define const_assert( E )    ((void) (const_assert_class<(E)>::i));

If the assertion fails, it tries to reference the non-existant member i
of the specialization _const_assert<0>. The result is an expression, so
it can be used conveniently.

Now use it in the example:

    // ...
    const_assert_class< i >= j > x;
    // ...

Or
    // ...
    void _assertions() { const_assert( i >= j ); }
    // ...

Or something along those lines

The conditional
> A <3,2> a1; // fine
> A <2,3> a2; // would not compile
>
>  --
> Jorma.Kuha@research.nokia.com

--
Chelly Green | mailto:chelly@eden.com | http://www.eden.com/~chelly


[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: christian.bau@isltd.insignia.com (Christian Bau)
Date: 1996/08/10
Raw View
In article <1996Aug09.121900.1751.54543@nrchub01he.research.nokia.com>,
kuha@research.nokia.com (Kuha Jorma NRC/Hki) wrote:

> Proposal: Division by zero in constant expression should not be allowed,
> resulting in compile error instead of undefined run-time behaviour.
>
> Motivation: This would allow us for example to check
> restrictions on template-arguments on compile-time like this:
>
> template<int i, int j>  class A
> //
> // "i>= j" should always hold
> {
>     enum {assertion1 = 1 / (i >= j) };
>     enum {assertion2 = (i >= j) ? 1 : 1/0 };  // would this be OK too?
> };
>
> A <3,2> a1; // fine
> A <2,3> a2; // would not compile

Instead, you can already write the following which should complain about
an array with 0 elements if i < j. However, I would very much like a
compile time version of assert that should be usable at any place a
declaration can be used.

template<int i, int j>  class A
//
// "i>= j" should always hold
{
    { char assertion1 [(i >= j]; };
};
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]