Std :: make_pair, C ++ 11 and explicit template parameters

Reedited: firstly, it is just a matter of curiosity, I know that std :: pair or many other solutions can fix this problem.

Can you tell me what exactly is behind this next problem? This code is a simple example working in C ++ 03 and crashes in C ++ 11.

    std::pair<int*,int**> getsth(int* param)
    {
        return std::make_pair<int*,int**>(param, 0);
    }

    int main(int argc, char* argv[])
    {
        int* a = new int(1);
        std::pair<int*,int**> par = getsth(a);
        std::cout << *par.first;
        return 0;
    }

I know how to fix this in order to be compatible with both standards here, but it will invalidate me that I don’t know what exactly make_pair is in this case.

Thank!

edited: compilation error message from Coliru:

main.cpp: In function 'std::pair<int*, int**> getsth(int*)':
main.cpp:8:47: error: no matching function for call to 'make_pair(int*&, int)'
     return std::make_pair<int*,int**>(param, 0);
                                               ^
main.cpp:8:47: note: candidate is:
In file included from /usr/local/include/c++/4.9.2/bits/stl_algobase.h:64:0,
                 from /usr/local/include/c++/4.9.2/bits/char_traits.h:39,
                 from /usr/local/include/c++/4.9.2/ios:40,
                 from /usr/local/include/c++/4.9.2/ostream:38,
                 from /usr/local/include/c++/4.9.2/iostream:39,
                 from main.cpp:1:
/usr/local/include/c++/4.9.2/bits/stl_pair.h:276:5: note: template<class _T1, class _T2> constexpr std::pair<typename std::__decay_and_strip<_Tp>::__type, typename std::__decay_and_strip<_T2>::__type> std::make_pair(_T1&&, _T2&&)
     make_pair(_T1&& __x, _T2&& __y)
     ^
/usr/local/include/c++/4.9.2/bits/stl_pair.h:276:5: note:   template argument deduction/substitution failed:
main.cpp:8:47: note:   cannot convert 'param' (type 'int*') to type 'int*&&'
     return std::make_pair<int*,int**>(param, 0);
                                               ^
main.cpp:9:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
+4
source share
2 answers

Links to Rvalue have been noted. Where std::make_pairin C ++ 03 has a signature

template< class T1, class T2 >
std::pair<T1,T2> make_pair( T1 t, T2 u );

In C ++ 11, it has

template< class T1, class T2 >
std::pair<V1,V2> make_pair( T1&& t, T2&& u );

V1 V2 () std::decay<T1|T2>::type. ++ 14 constexpr, .

, std::make_pair<int*, int**> ++ 03 int* int**, ++ 11 int*&& int**&&.

0 int**&& , param lvalue rvalue int*. ++ 11.

return std::make_pair<int*&, int**>(param, 0);

++ 03, ++ 11 - param int*&, std::pair<int*&, int**> std::pair<int*, int**>, .

, std::make_pair . @T.C. , , std::pair,

return std::pair<int*, int**>(param, 0);
+8

make_pair. .

, nullptr make_pair, 0.

+3

All Articles