私はテンプレートと転送を試していました。私を驚かせた簡単な実験コードを書いた。私はこの仕組みをよりよく理解したいと思っています。おそらく私はここでいくつかの知識が不足しているので、私は助けを求めます。以下のコードの2つの呼び出しがコンパイルされない理由を説明してください(場所2と3)。テンプレートからさまざまな種類の関数へのパラメータの転送
#include <iostream>
#include <memory>
#include <utility>
using namespace std;
void h2rvalref(int&& i) { cout << "h2rvalref" << endl; }
void h2ref(int& i) { cout << "h2ref" << endl; }
void h2val(int i) { cout << "h2val" << endl; }
template <class T, class X>
void h1(T&& t, X x) { x(forward<T>(t)); }
int main()
{
// PLACE (1)
h1<int, decltype(h2rvalref)>(1, h2rvalref);
auto b = 1;
// PLACE (2)
// h1<int, decltype(h2ref)>(b, h2ref); // --> ERROR - no matching function..., cannot convert 'b' (type 'int') to type 'int&&'
// PLACE (3)
// h1<int, decltype(h2val)>(b, h2val); // --> ERROR - no matching function..., cannot convert 'b' (type 'int') to type 'int&&'
}
エラーが、私はint型の値bを持っているとき& &をintにint型の変換について何かを言う、なぜ私は理解していません。
質問しているコードをコメントにしないでください。構文の強調表示により、読みにくくなります。 – Barmar
これは覚えています。 – YotKay