Topic: Unreachable code in a templated function instatiation


Author: W Karas <wkaras@yahoo.com>
Date: Thu, 19 Aug 2010 02:06:07 CST
Raw View
Should unreachable code in the instantiation of a templated function
cause an error or warning.  Here is an illustrative example:

template <int N, int D>
int safe_div(void)
 {
   if (D == 0)
     return(0);
   else
     return(N / D);
 }

const int Zero = safe_div<0, 0>();

Sholuld this result in a static arithmetic divide-by-zero error or
warning?

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: CornedBee <wasti.redl@gmx.net>
Date: Fri, 20 Aug 2010 16:58:23 CST
Raw View
On Aug 19, 1:06 am, W Karas <wka...@yahoo.com> wrote:
> Should unreachable code in the instantiation of a templated function
> cause an error or warning.  Here is an illustrative example:
>
> template <int N, int D>
> int safe_div(void)
>  {
>    if (D == 0)
>      return(0);
>    else
>      return(N / D);
>  }
>
> const int Zero = safe_div<0, 0>();
>
> Sholuld this result in a static arithmetic divide-by-zero error or
> warning?

Now why would it do that?

As far as the standard is concerned, there's no such thing as a
warning. There's just ill-formed programs and diagnostics. Ill-formed
programs are those that violate some compile-time rule of the language
(as opposed to undefined behavior, which comes from violating run-time
rules). Most things that lead to ill-formed programs require that a
diagnostic be emitted, but not all, since some of these things are
very hard to check (e.g. the ODR).

Back in the real world, these diagnostics are compile errors. Since
the code above is perfectly valid by all compile-time rules of C++,
emitting an error would be a compliance issue.
Warnings, on the other hand, are instances where the compiler believes
that the programmer is doing something wrong, but there is no definite
rule about it. They are a QoI issue. Now, if a compiler were to emit a
warning about the code above, it would be poor QoI, because the
division by zero it would be complaining about could never actually be
executed.

To sum up: emitting an error would be incorrect, and emitting a
warning would be stupid.

Sebastian


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: W Karas <wkaras@yahoo.com>
Date: Sat, 21 Aug 2010 21:58:51 CST
Raw View
On Aug 20, 6:58 pm, CornedBee <wasti.r...@gmx.net> wrote:
> On Aug 19, 1:06 am, W Karas <wka...@yahoo.com> wrote:
>
>
>
> > Should unreachable code in the instantiation of a templated function
> > cause an error or warning.  Here is an illustrative example:
>
> > template <int N, int D>
> > int safe_div(void)
> >  {
> >    if (D == 0)
> >      return(0);
> >    else
> >      return(N / D);
> >  }
>
> > const int Zero = safe_div<0, 0>();
>
> > Sholuld this result in a static arithmetic divide-by-zero error or
> > warning?
>
> Now why would it do that?
>
> As far as the standard is concerned, there's no such thing as a
> warning. There's just ill-formed programs and diagnostics. Ill-formed
> programs are those that violate some compile-time rule of the language
> (as opposed to undefined behavior, which comes from violating run-time
> rules). Most things that lead to ill-formed programs require that a
> diagnostic be emitted, but not all, since some of these things are
> very hard to check (e.g. the ODR).
>
> Back in the real world, these diagnostics are compile errors. Since
> the code above is perfectly valid by all compile-time rules of C++,
> emitting an error would be a compliance issue.
> Warnings, on the other hand, are instances where the compiler believes
> that the programmer is doing something wrong, but there is no definite
> rule about it. They are a QoI issue. Now, if a compiler were to emit a
> warning about the code above, it would be poor QoI, because the
> division by zero it would be complaining about could never actually be
> executed.
>
> To sum up: emitting an error would be incorrect, and emitting a
> warning would be stupid.
>
> Sebastian

I'm getting a warning, and I agree it's stupid.  But I think you're
correct that superfluous warnings are (unfortunately perhaps) not a
Standard issue.



--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]