xhawk18 / promise-cpp

C++ promise/A+ library in Javascript style.
MIT License
672 stars 92 forks source link

promise::any貌似不支持右值构造 #17

Open Cirnoo opened 2 years ago

Cirnoo commented 2 years ago
struct Test {
    Test() {
        printf("constructor\n");
    };

    Test(const Test &) {
        printf("copy constructor\n");
    }

    Test(Test && m) {
        printf("move constructor\n");
    }

    ~Test() {
        printf("destruct \n");
    }

};
int main() {
    promise::any a(Test{});
//    输出
//    constructor
//    copy constructor
//    destruct
//    destruct
}

C++17 的std::any是正常的,输出

constructor
move constructor
destruct 
destruct 

看起来是holder类没有对右值构造进行重载。

template<typename ValueType>
    class holder : public placeholder {
    public: // structors
        holder(const ValueType & value)
            : held(value) {
        }

    public: // queries
        virtual type_index type() const {
            return type_id<ValueType>();
        }

        virtual placeholder * clone() const {
            return new holder(held);
        }

        virtual any call(const any &arg) const {
            return any_call(held, arg);
        }
    public: // representation
        ValueType held;
    private: // intentionally left unimplemented
        holder & operator=(const holder &);
    };
xhawk18 commented 2 years ago

确实!是没有支持