How can I convert std::pair<const string,int>
to std::pair<string,int>
without actually making a new pair?
I tried using static_cast as below:
static_cast<std::pair<const string, int>>(curr->valPair)
curr->valPair - is returning std::pair<string,int>
But it is returning error:
invalid initialization of non-const reference of type ‘std::pair<const std::basic_string<char>, int>&’ from an rvalue of type ‘std::pair<const std::basic_string<char>, int>’
[Edit: In short the question is how to convert std::pair<string,int>
to std::pair<const string,int>
]
Edit2:
Adding a small piece of code to reproduce the issue
#include<iostream>
using namespace std;
class A
{
std::pair<int,int> valPair;
public:
std::pair<const int,int> & getPairByReference();
};
std::pair<const int,int> & A::getPairByReference()
{
return valPair;
}
int main()
{
A a;
a.getPairByReference();
return 0;
}
I am getting below error while compiling it:
try-5.cpp: In member function ‘std::pair<const int, int>& A::getPairByReference()’:
try-5.cpp:15:12: error: invalid initialization of reference of type ‘std::pair<const int, int>&’ from expression of type ‘std::pair<int, int>’