Topic: Undefined behavior or diagnostic required for illegal template instantiation.
Author: Hyman Rosen <hymie@prolifics.com>
Date: 1999/10/22 Raw View
James.Kanze@dresdner-bank.com writes:
> Is the compiler required to generate a diagnostic for the following
> program, or is it undefined behavior?
17.4.3.6/1 says
"In certain cases (replacement functions, handler functions,
operations on types used to instantiate standard library
template components), the C++ Standard Librray depends on
components supplied by a C++ program. If these components
do not meet their requirements, the Standard places no
requirements on the implementation."
---
[ 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: James.Kanze@dresdner-bank.com
Date: 1999/10/21 Raw View
Is the compiler required to generate a diagnostic for the following
program, or is it undefined behavior?
#include <vector>
#include <iostream>
#include <iterator>
static int tbl[] =3D { 1 , 5 , 120 } ;
class DummyIter
{
public:
typedef int value_type ;
typedef ptrdiff_t difference_type ;
typedef int const* pointer ;
typedef int const& reference ;
typedef std::forward_iterator_tag
iterator_category ;
DummyIter( int i ) : courant( tbl+ i ) {}
int const& operator*() const { return *courant ; }
DummyIter& operator++() { ++ courant ; return *this ; }
bool operator=3D=3D( DummyIter const& other ) cons=
t {
return courant =3D=3D other.courant ; }
private:
int const* courant ;
} ;
int
main()
{
std::vector< int > t( DummyIter( 0 ) , DummyIter( 3 ) ) ;
for ( unsigned i =3D 0 ; i < t.size() ; ++ i ) {
std::cout << t[ i ] << '\n' ;
}
return 0 ;
}
The error, of course, is that there is no matching constructor for the
vector in main, since DummyIter does not meet the requirements of an
InputIterator, nor is it convertible to std::vector::size_type.
My own opinion is that this is undefined behavior. DummyIter fails to
meet the syntax requirements for an iterator, however (no !=3D nor postfi=
x
++), and at least one of the experts in the French group believes that
this makes the error a syntax error, requiring a diagnostic. While I
can't believe that this was the actual intent, I can't find anything in
the standard which contradicts him either.
As a quality of implementation issue, I would expect most compilers will
either accept it and generate code so that it works, or signal an
error. The only compiler I tried (g++ 2.95, version Windows NT) accepts
it, and I imagine that most others do too.
As a side issue: would it be worth introducing another category of error
for such cases? I really don't like the idea that the compiler could
legally format my hard disk because of this error, but I can't seen
requiring a diagnostic, and the program certainly isn't, nor can it be,
legal. It doesn't seem too hard to me to introduce a category of error
for instantiations of the standard library templates, which says that if
template is instantiated over a type which doesn't meet the syntactical
requirements of the template, it is undefined whether the code works or
you get a diagnostic, but no other alternatives are possible.
--
James Kanze mailto:James.Kanze@dresdner-bank.com
Conseils en informatique orient=E9e objet/
Beratung in objekt orientierter Datenverarbeitung
Ziegelh=FCttenweg 17a, 60598 Frankfurt, Germany Tel. +49(069)63198627
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 ]