lundi 1 mars 2021

Why I cannot use std::tie to unpack values from vector of pair in range based for loop

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 =;

    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;
}

Aucun commentaire:

Enregistrer un commentaire