Why can't I do this?
#include <iostream> #include <fstream> #include <vector> using namespace std; template <class Type, template <class, class = std::allocator<Type> > class Container> void print_container(Container<Type>&& container, ostream& os) { os << "{ "; for (auto& var : container) { os << var << " "; } os << "}"; } int main(int argc, char* argv[]) { std::vector<string> name; name.push_back("Igor"); name.push_back("Andrii"); print_container(name, cout); //print_container(vector<string>{"Igor", "Andrii"}, cout); // Работает без ошибок return 0; } I get errors:
example.cpp:22:31: error: cannot bind 'std::vector<std::basic_string<char> >' lvalue to 'std::vector<std::basic_string<char> >&&' print_container(name, cout); ^ example.cpp:8:6: note: initializing argument 1 of 'void print_container(Container<Type>&&, std::ostream&) [with Type = std::basic_string<char>; Container = std::vector; std::ostream = std::basic_ostream<char>]' void print_container(Container<Type>&& container, ostream& os) {