[请教] android::hardware::Return 无法使用拷贝构造函数,怎么办? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
请不要在回答技术问题时复制粘贴 AI 生成的内容
amiwrong123
V2EX    程序员

[请教] android::hardware::Return 无法使用拷贝构造函数,怎么办?

  •  
  •   amiwrong123 2019-10-24 18:32:00 +08:00 2970 次点击
    这是一个创建于 2257 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在编写 c++的单体测试,但是在用 mock 的时候出了点问题。

    //在 mock 类里面,函数已经被 mock 了 MOCK_METHOD1(unRegisterListener, Return<EnCommonFuncResult>(EnCommonListenerID)); //unRegisterListener 的函数签名是 virtual Return<EnMeterFuncResult> unRegisterListener(EnMeterListenerID serviceId)//这个 Return 是 android::hardware::Return //现在想进行插桩,EnCommonFuncResult 就是一个 enum class EnCommonFuncResult : uint8_t Return<EnCommonFuncResult> returnOk= EnCommonFuncResult::EN_COMMON_RESULT_OK;//这个 Return 是 android::hardware::Return EXPECT_CALL(mockICommon, unRegisterListener(_)).WillOnce(::testing::Return(returnOk));//这儿出错 //以下为报错信息 error: call to implicitly-deleted copy constructor of 'android::hardware::Return<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult>' system/libhidl/base/include/hidl/Status.h:200:5: note: copy constructor is implicitly deleted because 'Return<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult>' has a user-declared move constructor Return(Return &&other) = default; 

    抱歉 c++有点菜,实在没搞懂这个 android::hardware::Return 该怎么用

    第 1 条附言    2019-10-24 21:20:15 +08:00
    ```
    改成用这个,也报错:
    EXPECT_CALL(mockICommon, unRegisterListener(_)).WillOnce(::testing::Return(EnCommonFuncResult::EN_COMMON_RESULT_OK));

    以下是报错信息:
    error: call to implicitly-deleted copy constructor of 'testing::internal::ReturnAction<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult>::Impl<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult, android::hardware::Return<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult> (iauto::hardware::ucom::common::V1_0::EnCommonListenerID)>::Result' (aka 'android::hardware::Return<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult>')
    virtual Result Perform(const ArgumentTuple&) { return value_; }
    ^~~~~~
    external/googletest/googlemock/include/gmock/gmock-actions.h:575:14: note: in instantiation of member function 'testing::internal::ReturnAction<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult>::Impl<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult, android::hardware::Return<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult> (iauto::hardware::ucom::common::V1_0::EnCommonListenerID)>::Perform' requested here
    explicit Impl(const linked_ptr<R>& value)
    ^
    external/googletest/googlemock/include/gmock/gmock-actions.h:557:26: note: in instantiation of member function 'testing::internal::ReturnAction<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult>::Impl<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult, android::hardware::Return<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult> (iauto::hardware::ucom::common::V1_0::EnCommonListenerID)>::Impl' requested here
    return Action<F>(new Impl<R, F>(value_));
    ^


    ```
    amiwrong123
        1
    amiwrong123  
    OP
       2019-10-24 21:40:08 +08:00
    ```cpp
    template<typename T> class Return : public details::return_status {
    private:
    T mVal {};
    public:
    Return(T v) : details::return_status(), mVal{v} {}
    Return(Status s) : details::return_status(s) {}

    // move-able.
    // precondition: "this" has checked status
    // postcondition: other is safe to destroy after moving to *this.
    Return(Return &&other) = default;
    Return &operator=(Return &&) = default;

    ~Return() = default;

    operator T() const {
    assertOk();
    return mVal;
    }

    T withDefault(T t) {
    return isOk() ? mVal : t;
    }
    };
    ```
    这是 android::hardware::Return 的源码,因为有这句 Return(Return &&other) = default;,我就不能调用拷贝构造函数了,因为被隐式删除了。

    求大佬解答了
    amiwrong123
        2
    amiwrong123  
    OP
       2019-10-24 22:19:20 +08:00
    error: call to implicitly-deleted copy constructor of 'testing::internal::ReturnAction<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult>::Impl<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult, android::hardware::Return<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult> (iauto::hardware::ucom::common::V1_0::EnCommonListenerID)>::Result' (aka 'android::hardware::Return<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult>')
    virtual Result Perform(const ArgumentTuple&) { return value_; }
    ^~~~~~
    external/googletest/googlemock/include/gmock/gmock-actions.h:575:14: note: in instantiation of member function 'testing::internal::ReturnAction<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult>::Impl<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult, android::hardware::eturn<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult> (iauto::hardware::ucom::common::V1_0::EnCommonListenerID)>::Perform' requested here
    explicit Impl(const linked_ptr<R>& value)
    ^
    external/googletest/googlemock/include/gmock/gmock-actions.h:557:26: note: in instantiation of member function 'testing::internal::ReturnAction<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult>::Impl<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult, android::hardware::Return<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult> (iauto::hardware::ucom::common::V1_0::EnCommonListenerID)>::Impl' requested here
    return Action<F>(new Impl<R, F>(value_));
    ^
    TestSender.cpp:54:66: note: in instantiation of function template specialization 'testing::internal::ReturnAction<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult>::operator Action<android::hardware::Return<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult> (iauto::hardware::ucom::common::V1_0::EnCommonListenerID)>' requested here
    EXPECT_CALL(mockICommon, unRegisterListener(_)).WillOnce(::testing::Return(EnCommonFuncResult::EN_COMMON_RESULT_OK));
    ^
    system/libhidl/base/include/hidl/Status.h:200:5: note: copy constructor is implicitly deleted because 'Return<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult>' has a user-declared move constructor
    Return(Return &&other) = default;

    给一下附言 1 的完整错误信息吧。

    可能是因为 gmock 的::testing::Return 的内部逻辑吧,导致我这个地方总是会调用到 android::hardware::ReturnReturn<EnComdCommonFuncResult>。gmock 的源码我也去看了,但是太难懂了。快哭了
    billyzs
        3
    billyzs  
       2019-10-24 23:44:11 +08:00
    试试
    ```
    Return<EnCommonFuncResult> returnOk{EnCommonFuncResult::EN_COMMON_RESULT_OK}
    ```
    xiaoloudoufu
        4
    xiaoloudoufu  
       2019-10-25 10:07:52 +08:00
    你可以看一下文档:https://github.com/google/googletest/blob/master/googlemock/docs/cook_book.md ,对于 move-only 的类型应该要使用 ByMove()
    amiwrong123
        5
    amiwrong123  
    OP
       2019-10-25 10:55:58 +08:00 via Android
    @xiaoloudoufu
    谢谢,我也是刚看见有这个,等会我试试
    amiwrong123
        6
    amiwrong123  
    OP
       2019-10-25 12:13:01 +08:00
    @xiaoloudoufu
    果然 ,用了这个就好了,怪自己没好好文档了
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5233 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 26ms UTC 01:33 PVG 09:33 LAX 17:33 JFK 20:33
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86