Home > Back-end >  C value_type::second_type compiler error inside template
C value_type::second_type compiler error inside template

Time:09-17

I get compiler error when using decltype inside template function. Example is pretty self-explanatory. Help?

template<class T>
void foo(T&& m)
{
    auto t = (decltype(m)::value_type::second_type::value_type*)3; // compiler error
}

int main()
{
    unordered_map<int, map<float, double>> m;
    foo(m);
    auto t = (decltype(m)::value_type::second_type::value_type*)3; // ok, t is a std::pair<const float, double>*
}

CodePudding user response:

If you use m in the function, you need to remove the reference (and add typename):

Example:

typename std::remove_reference_t<decltype(m)>::value_type::second_type::value_type* t;

Or simply use T:

typename T::value_type::second_type::value_type* t;
  • Related