G
Gianni Mariani
I posted a gcc bug
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13332
here is the text:
-----------------------------------------------------------
This code will compile fine but fail on a linker error.
templ_examp.cpp:41: undefined reference to `Bounds<float,
(unsigned)4>::size'
However if "static const unsigned size" is changed from an "unsigned" to an
"int" or "short", it appears to work correctly.
template <typename T, unsigned N>
class Bounds
{
public:
static const unsigned size = N;
T * ptr;
T & operator[] ( unsigned i )
{
if ( i >= size )
{
throw "Array outa bounds";
}
return ptr[ i ];
}
Bounds( T ( & i_ptr)[N] )
: ptr( i_ptr )
{
}
};
template <typename T, unsigned N>
Bounds<T,N> make_bounds( T ( & i_ptr)[N] )
{
return Bounds<T,N>( i_ptr );
}
float array[] = { 1., 2., 3., 4. };
#include <iostream>
int main()
{
std::cout << make_bounds( array )[ 2 ];
std::cout << make_bounds( array ).size;
// std::cout << make_bounds( array )[ 20 ]; // throw exception
}
------- Additional Comment #1 From Andrew Pinski 2003-12-06 08:18
[reply] -------
From bug 13259 which this is bug is a dup of:
The standard says that you may inline-initialize the static variable,
but you still need to _define_ it somewhere so that a memory location
is assigned to it.
-----------------------------------------------------------
if below is not a definition:
....
static const unsigned size = N;
....
what is it ?
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13332
here is the text:
-----------------------------------------------------------
This code will compile fine but fail on a linker error.
templ_examp.cpp:41: undefined reference to `Bounds<float,
(unsigned)4>::size'
However if "static const unsigned size" is changed from an "unsigned" to an
"int" or "short", it appears to work correctly.
template <typename T, unsigned N>
class Bounds
{
public:
static const unsigned size = N;
T * ptr;
T & operator[] ( unsigned i )
{
if ( i >= size )
{
throw "Array outa bounds";
}
return ptr[ i ];
}
Bounds( T ( & i_ptr)[N] )
: ptr( i_ptr )
{
}
};
template <typename T, unsigned N>
Bounds<T,N> make_bounds( T ( & i_ptr)[N] )
{
return Bounds<T,N>( i_ptr );
}
float array[] = { 1., 2., 3., 4. };
#include <iostream>
int main()
{
std::cout << make_bounds( array )[ 2 ];
std::cout << make_bounds( array ).size;
// std::cout << make_bounds( array )[ 20 ]; // throw exception
}
------- Additional Comment #1 From Andrew Pinski 2003-12-06 08:18
[reply] -------
From bug 13259 which this is bug is a dup of:
The standard says that you may inline-initialize the static variable,
but you still need to _define_ it somewhere so that a memory location
is assigned to it.
-----------------------------------------------------------
if below is not a definition:
....
static const unsigned size = N;
....
what is it ?