"const" doesn't help you in the presence of aliasing:
#include <iostream>
#include <vector>
template<typename T,typename U>
void print(const T& v, U& w) {
for (auto ii = v.begin(), ie = v.end(); ii != ie; ++ii) {
w.clear();
std::cout << *ii << std::endl;
}
}
int main() {
std::vector<std::string> v;
v.push_back(std::string("Hello"));
v.push_back(std::string("there"));
print(v, v);
return 0;
}
In general there are lots of ways to get non-const access to const data. You'd need a sophisticated form of whole-program alias analysis to eliminate the unsafety here. (Even if you didn't have the second "U& w" parameter, there are other potential ways that "print" could get non-const access to "v"; global variables, TLS, by accessing a member of "v", etc.)