0

以下のコードでは:

template<class Key, class Value>
    class Pair
    {
    private:
        std::pair<Key,Value> body_;
    public:
        //No cpy ctor - this generated by compiler is OK
        Pair(Key&& key,Value&& value)
        {
            body_.first = key;
            body_.second = value;
        }

        Pair(Pair<Key,Value>&& tmpPattern)
        {
            body_.swap(tmpPattern.body_);
        }

        Pair& operator=(Pair<Key,Value> tmpPattern)
        {
            body_.swap(tmpPattern.body_);
            return *this;
        }

                };

    template<class Key, class Value>
    Pair<Key,Value> MakePair(Key&& key, Value&& value)
    {
        return Pair<Key,Value>(key,value);
    }

MakePair を実行しようとすると、奇妙な理由でエラーが発生します。なぜですか? 神のみぞ知る...

int main(int argc, char** argv)
{
auto tmp = MakePair(1, 2);
}

これは次のエラーです:
エラー エラー C2665: Pair::Pair' : none of the 3 overloads could convert the all argument types

どの変換を実行する必要があるのか​​ わかりませんか?

4

2 に答える 2

0

タイプではなく、に値を渡す必要がありMakePairます。これを試して:

int a = 1;
int b = 2;
auto tmp = MakePair( a, b ); // creates a Pair<int,int> with the values of a and b
于 2010-11-22T12:27:26.287 に答える
0

return Pair<Key,Value>(std::forward<Key>(key),std::forward<Value>(value));

右辺値転送が暗黙的ではない理由はよくわかりませんが。

編集:ああ、私はそれを得たと思います。このようにして、値を取る関数に右辺値参照を渡すことができます。

于 2010-11-22T16:28:33.280 に答える