Why no conversion?
#include <ostream>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <string>
// add any I've forgotten
template < typename T >
class portable_cref_buffer
{
const T* itsData;
size_t itsSize;
public:
// just one of its constructors. Note there is no constructor from T*
alone
template < size_t N >
portable_cref_buffer( const T (&arr)[N] )
: itsData( arr ), itsSize( N )
{
}
const T* begin() const
{
return itsData;
}
const T* end() const
{
return itsData + itsSize;
}
};
void output( const portable_cref_buffer< std::string > & words )
{
std::copy( words.begin(), words.end(),
std::ostream_iterator<std::string>( std::cout, " " ) );
std::cout << '\n';
}
int main()
{
std::string vals[ 3 ];
vals[0] = "Hello";
vals[1] = "World";
vals[2] = "!";
output( vals );
}
Fails to compile on Comeau tryitout (as well as on gcc3.2.1) with
"ComeauTest.c", line 47: error: no suitable constructor exists to
convert from
"std::string [3]" to "portable_cref_buffer<std::string>"
Makes no difference if I put an explicit
portable_cref_buffer<std::strnig> in main thus
output( portable_cref_buffer<std::string>(vals );
I know that the parameter in portable_cref_buffer indicates const but
that is because
1. it can take an array to const and
2. it isn't going to modify them, nor allow any modification to them
via the class interface.
If it were a pointer it would allow the conversion to const. Why is it
not allowing it here?
(I have had to add an extra constructor to my portable_cref_buffer
template with a non-const parameter. Why is this necessary?)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]