Incomplete type in template parameter, complete type as argument
I've got a somewhat strange situtation, and while I have a
solution, I'm curious about the legality of my first attempted
solution, purely templates. (G++ doesn't compile it, but I
can't decide whether the problem is in my code, or a bug in
g++). Basically, the code is:
template< typename T, size_t N >
T*
begin( T (&array)[ N ] )
{
return array ;
}
template< typename T, size_t N >
T*
end( T (&array)[ N ] )
{
return array + N ;
}
struct Match
{
explicit Match( char const* key )
: myKey( key )
{
}
bool operator()( char const* lhs ) const
{
return strcmp( lhs, myKey ) == 0 ;
}
char const* myKey ;
} ;
template< char const* (&keys)[] >
bool
accept(
char const* key )
{
return std::find_if( begin( keys ), end( keys ),
Match( key ) )
!= end( keys ) ;
}
char const* tab1[] =
{
"Lu", "Ll", "Lt", "Lm", "Lo", "Nd", "Nl", "No"
} ;
char const* tab2[] =
{
"Lu", "Ll", "Lt", "Lm", "Lo"
} ;
struct Table
{
int id ;
bool (* fnc)( char const* key ) ;
} ;
Table const tbl[] =
{
{ 1, &accept< tab1 > },
{ 2, &accept< tab2 > },
} ;
G++ complains because, if I interpret the error messages
correctly, it still consideres the type of keys as incomplete in
the instantiations of "accept" (which causes problems when it
attempts to instantiate "begin" and "end"), although the
instantiation arguments have complete types. (The code works if
the template parameters of "accept" are "template< size_t N,
char const* (&keys)[ N ] >", and I explicitly pass the length of
the array.) On one hand, I can see its point, but on the
other... if it really wants to complain, it shouldn't allow me
to instantiate the template with a complete type either:-). It
is the nature of incomplete types to be completed.
FWIW: I can't find anything in the standard concerning the
handling of incomplete types in template non-type parameters.
G++ also complains about:
struct T ;
template< T const* p >
bool
f()
{
return p->ok() ;
}
struct T
{
bool ok() const { return true ; }
} ;
T anA ;
bool (*pf)() = &f< &anA > ;
Which really seems like it should be legal.
--
James Kanze (Gabi Software) email: james.kanze@gmail.com
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
---
[ 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://www.comeaucomputing.com/csc/faq.html ]