Reputation: 125
Can please someone explain why this compilation error.
Error: function template "std::tie" is not a type name This is working fine. tie(str1,str2) = pairval;
#include<iostream>
#include<vector>
#include<tuple>
using namespace std;
int main()
{
vector<pair<string,string>>v ={{"Hello", "Task"}};
pair<string, string> p = {"Hello", "Task"};
string str1, str2;
for(auto & pairval : v)
{
tie(str1,str2) = pairval;
cout<<pairval.first<<" "<<pairval.second<<endl; //working
}
for(auto & [str1,str2]: v)
{
cout<<str1<<" "<<str2<<endl; //working
}
for(tie(str1, str2):v) // compilation error : function template "std::tie" is not a type name
{
cout<<str1<<" "<<str2<<endl;
}
return 0;
}
Upvotes: 1
Views: 851
Reputation: 4713
Checkout the range-for-loop definition:
for ( range_declaration : range_expression ) loop_statement
here range_declaration
needs to be a declaration. std::tie(str1,str2)
is not a declaration. Declaration is something like int x
or auto&& y
.
See cppreference for range-for-loop https://en.cppreference.com/w/cpp/language/range-for and https://en.cppreference.com/w/cpp/language/declarations for declarations.
Upvotes: 4