开发者

Strange problem with conversion

In code below:

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)
    {
        r开发者_如何学Goeturn Pair<Key,Value>(key,value);
    }

For some bizzare reason I'm getting error when I try to run MakePair, why? God knows...

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

This is this error:

Error error C2665: Pair::Pair' : none of the 3 overloads could convert all the argument types

I just don't understand what conversion there is to be performed?


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

Although I'm not really sure why rvalue forwarding isn't implicit.

Edit: Oh, I guess I got it. This way you can still pass a rvalue reference to a function that takes value.


You need to pass values to MakePair, not types. Try this:

int a = 1;
int b = 2;
auto tmp = MakePair( a, b ); // creates a Pair<int,int> with the values of a and b
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜