Topic: Is || short-ciruit at compile-time?


Author: =?ISO-8859-1?Q?J=F6rg?= Barfurth <joerg.barfurth@attglobal.net>
Date: 2000/07/18
Raw View
Am 12.07.00, 08:54:38, schrieb Jim Hyslop <jim.hyslop@leitch.com> zum The=
ma=20
Re: Is || short-ciruit at compile-time?:


> In article <t7ya3byre1.fsf@calumny.jyacc.com>,
>   Hyman Rosen <hymie@prolifics.com> wrote:
> > vaps4bm@prism.gatech.edu (Brian McNamara!) writes:
> > > I can't decide if the mere act of naming Foo<n>::x should cause the
> > > compiler to blow its recursive-template-stack or not.
> >
> > It should.
> I don't agree.  The compiler can certainly detect that the first operan=
d
> to the logical or operator (assuming, by the way, that the operator has
> not been overloaded) will always evaluate true, therefore the second
> operand will never be evaluated, therefore by the "as if" rule it is
> free to omit expanding the template.

> Unless there's something I've overlooked?

IMHO, the code is still considered 'potentially evaluated' and therefore=20
must be well-formed.
And, concerning the template, it is a 'point of instantiation'=20
independently of whether its value will be used.=20

The compiler must check for well-formedness and must instantiate the=20
template. Combining these requirements should lead to blowing the=20
compiler's facilities for instantiating recursive templates.

--
J=F6rg


---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: vaps4bm@prism.gatech.edu (Brian McNamara!)
Date: 2000/07/11
Raw View
vaps4bm@prism.gatech.edu (Brian McNamara!) once said:
>Put another way, is this program well-formed?
>
>   template <int N>
>   struct Foo {
>      enum { x = Foo<N-1>::x };      // infinite recursion
>   };
>
>   struct Bar {
>      enum { x = 1 || Foo<100>::x }; // short-circuit evaluation?
>   };
>
>   int main() {
>   }
>
>I can't decide if the mere act of naming Foo<n>::x should cause the
>compiler to blow its recursive-template-stack or not.

(The above "blows up" in g++2.95.1, btw.)

I should have mentioned that, if the program above is _not_
well-formed, that this is merely an inconvenience.  We can always use a
static-meta-thunk to effect the lazy evaluation if necessary (see code
below).  I am asking only because the above is much shorter and more
convenient.  The alternative (which I'm rather sure is legal) is shown
here (and compiles fine with g++):

#include <iostream>

//////////////////////////////////////////////////////////////////////
// Usual meta-IF
//////////////////////////////////////////////////////////////////////
template <bool b, class T, class E>
struct IF;

template <class T, class E>
struct IF<true,T,E> {
   typedef T RET;
};

template <class T, class E>
struct IF<false,T,E> {
   typedef E RET;
};

//////////////////////////////////////////////////////////////////////
// A way to thunk an int; not useful by itself, but useful to make then
// and else clauses of IF parallel
//////////////////////////////////////////////////////////////////////
template <int N>
struct IntHolderThunk {
   template <class Dummy>
   struct Value {
      static const int RET = N;
   };
};

//////////////////////////////////////////////////////////////////////
// Example of computation that may "blow up" at compile-time
//////////////////////////////////////////////////////////////////////
template <int N>
struct Foo {
   enum { x = Foo<N-1>::x };
};

//////////////////////////////////////////////////////////////////////
// Thunk the unsafe computation
//////////////////////////////////////////////////////////////////////
template <int N>
struct FooThunk {
   template <class Dummy>
   struct Value {
      static const int RET = Foo<N>::x;
   };
};

//////////////////////////////////////////////////////////////////////
// How to safely access the alternative
//////////////////////////////////////////////////////////////////////
struct Bar {
   typedef IF<1,IntHolderThunk<1>,FooThunk<100> > RET;
   enum { x = RET::RET::Value<int>::RET };
};

int main() {
   cout << Bar::x << endl;
}

--
Brian McNamara

---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Jim Hyslop <jim.hyslop@leitch.com>
Date: 2000/07/12
Raw View
In article <t7ya3byre1.fsf@calumny.jyacc.com>,
  Hyman Rosen <hymie@prolifics.com> wrote:
> vaps4bm@prism.gatech.edu (Brian McNamara!) writes:
> > I can't decide if the mere act of naming Foo<n>::x should cause the
> > compiler to blow its recursive-template-stack or not.
>
> It should.
I don't agree.  The compiler can certainly detect that the first operand
to the logical or operator (assuming, by the way, that the operator has
not been overloaded) will always evaluate true, therefore the second
operand will never be evaluated, therefore by the "as if" rule it is
free to omit expanding the template.

Unless there's something I've overlooked?

--
Jim
I ignore all email from recruitment agencies.  Except that
I reserve the right to send rude, nasty replies to recruiters.
Please do not send me email with questions - post
here.


Sent via Deja.com http://www.deja.com/
Before you buy.

---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: vaps4bm@prism.gatech.edu (Brian McNamara!)
Date: 2000/07/10
Raw View
Put another way, is this program well-formed?

   template <int N>
   struct Foo {
      enum { x = Foo<N-1>::x };      // infinite recursion
   };

   struct Bar {
      enum { x = 1 || Foo<100>::x }; // short-circuit evaluation?
   };

   int main() {
   }

I can't decide if the mere act of naming Foo<n>::x should cause the
compiler to blow its recursive-template-stack or not.

--
Brian McNamara

---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Hyman Rosen <hymie@prolifics.com>
Date: 2000/07/10
Raw View
vaps4bm@prism.gatech.edu (Brian McNamara!) writes:
> I can't decide if the mere act of naming Foo<n>::x should cause the
> compiler to blow its recursive-template-stack or not.

It should.

---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: dwalker07@snet.net.invalid (Daryle Walker)
Date: 2000/07/11
Raw View
Brian McNamara! <vaps4bm@prism.gatech.edu> wrote:

> Put another way, is this program well-formed?
>
>    template <int N>
>    struct Foo {
>       enum { x = Foo<N-1>::x };      // infinite recursion
>    };

Shouldn't you end the recursion with a definate template:

template <>
struct Foo<INT_MIN>
{
    enum { x = INT_MIN };
};

--
Daryle Walker
Mac, Internet, and Video Game Junkie
dwalker07 AT snet DOT net

---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]