-std = C++ 11を有効にして、g ++ 4.7(後のスナップショットの1つ)で再生していました。私は既存のコードベースの一部をコンパイルしようとしましたが、1つのケースはやや混乱しました。C++ 11指定されたテンプレートパラメータを持つmake_pairがコンパイルされない
誰かが何が起こっているのかを説明できるのであれば、私は感謝します。
ここで(私は種類を指定した場合、その後、私は同様に使用することがあります(3))私はmake_pairがであることを理解し、コード
#include <utility>
#include <iostream>
#include <vector>
#include <string>
int main ()
{
std::string s = "abc";
// 1 ok
std::pair < std::string, int > a = std::make_pair (s, 7);
// 2 error on the next line
std::pair < std::string, int > b = std::make_pair < std::string, int > (s, 7);
// 3 ok
std::pair < std::string, int > d = std::pair < std::string, int > (s, 7);
return 0;
}
は、(1)ケースとして使用されることをを意味します、しかし、この場合、なぜ失敗するのか分かりません。
正確なエラーがある:再び
test.cpp: In function ‘int main()’: test.cpp:11:83: error: no matching function for call to ‘make_pair(std::string&, int)’ test.cpp:11:83: note: candidate is: In file included from /gcc4.7/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.0/../../../../include/c++/4.7.0/utility:72:0, from test.cpp:1: /gcc4.7/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/stl_pair.h:274:5: note: template constexpr std::pair::__type, typename std::__decay_and_strip<_T2>::__type> std::make_pair(_T1&&, _T2&&) /gcc4.7/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/stl_pair.h:274:5: note: template argument deduction/substitution failed: test.cpp:11:83: note: cannot convert ‘s’ (type ‘std::string {aka std::basic_string}’) to type ‘std::basic_string&&’
、ここで問題は、単に「何が起こっているの?」ています私はテンプレート仕様を削除することで問題を解決できることを知っていますが、ここでは何が失敗しているのかを知りたいだけです。前もって感謝します。
EDIT:
- グラム++ 4.4は問題なく、このコードをコンパイル。
- -std = C++ 11の削除も問題なくコードでコンパイルされます。
優れた質問です。 C++ 11の微妙な改変のもう1つの例は、[std :: vector'構造の改変](http://stackoverflow.com/questions/5759232/stdvector-default-construction-c11-そして改革 - 変化)。少なくともこの1つは、コンパイラエラーをもたらし、セマンティクスの静かな変更ではありません。 –
私は整数変数iを持っています。私はiと別のオブジェクトとペアを作りたい。どのくらい私はmakepairを呼び出す必要があります。 1)make_pair <*i, obj> 2)int && j = i; make_pair?両方とも動作していません。それを行う正しい方法は何ですか? –
PHcoDer