const qualifiers cannot be applied to '&'
Code:
#include <list>
template
<
typename T
>
class Foo
{
T container;
typedef typename T::iterator iterator;
struct Bar
{
typedef decltype(*container.begin()) T_type;
T_type t;
iterator* index;
Bar
( const T_type& t,
iterator* index = 0
) : t(t),
index(index)
{ }
};
std::list<Bar> bars;
public:
Foo<T>(const T& src);
};
template
<
typename T
>
Foo<T>::Foo(const T& src)
{
typename T::const_iterator it = src.begin(), end = src.end();
for( ; it != end; ++it )
{
Bar b(*it);
bars.push_back(b);
}
}
class test{ };
int main()
{
std::list<test> list;
Foo<std::list<test>> foo(list);
return 0;
}
I'm trying to copy the elements of the container into wrapper objects, but I keep getting the error:
Code:
error: 'const' qualifiers cannot be applied to 'test&'|
Removing const from the constructor arguments (and the reference to T_type t in Bar's constructor) allows me to compile, but I don't like the idea of providing an interface that doesn't guarantee the user's original container won't be modified.