C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c – std :: function:对参数进行严格的编译时验证大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想实现一个类,它包含两个带有预定义函数签名的回调.

该类具有模板化的ctor,它使用std :: bind来创建std :: function成员.我预计如果将具有错误签名的函数传递给ctor,编译器(g 4.6)会抱怨.但是,编译器接受以下内容

callBACk c1(i,&test::func_a,&test::func_a);

我能理解为什么会那样做.我试图为static_assert构造一个合适的条件但没有成功.

如何防止出现编译时错误

#include <functional>

using namespace std::placeholders;

class callBACk {
public:
    typedef std::function<bool(const int&)>     type_a;
    typedef std::function<bool(int&)>       type_b;

    template <class O,typename CA,typename CB>
        callBACk(O inst,CA ca,CB cb)
        : 
        m_ca(std::bind(ca,inst,_1)),m_cb(std::bind(cb,_1))
        { }

private:
    type_a  m_ca;
    type_b  m_cb;
};


class test {
public:
    bool func_a(const int& arg) { return true; }
    bool func_b(int& arg) { arg = 10; return true; }
};

int main()
{
    test i;
    callBACk c(i,&test::func_b);

// Both should fail at compile time

    callBACk c1(i,&test::func_a);
//  callBACk c2(i,&test::func_b,&test::func_b);

    return 0;
}

更新:来自访客的回答解决了我最初的问题.不幸的是,我有一堆相关的案例需要解决,这些案例通过以下代码(http://ideone.com/P32sU)进行了演示:

class test {
public:
    virtual bool func_a(const int& arg) { return true; }
    virtual bool func_b(int& arg) { arg = 10; return true; }
};

class test_d : public test {
public:
    virtual bool func_b(int& arg) { arg = 20; return true; }
};

int main()
{
    test_d i;
    callBACk c(i,&test_d::func_a,&test_d::func_b);
    return 0;
}

函数签名有效,但是在这种情况下会触发访问者建议的static_assert:

prog.cpp: In constructor 'callBACk::callBACk(O,CA,CB) [with O = test_d,CA = bool (test::*)(const int&),CB = bool (test_d::*)(int&)]':
prog.cpp:41:51:   instantiated from here
prog.cpp:17:12: error: static assertion Failed: "First function type incorrect"

我认为最好只比较函数参数和返回值.请建议如何.

谢谢.

解决方法

您可以在构造函数体中静态断言:

static_assert(std::is_same<CA,bool(O::*)(const int&)>::value,"First function type incorrect");
static_assert(std::is_same<CB,bool(O::*)(int&)>::value,"Second function type incorrect");

见:http://ideone.com/u0z24

大佬总结

以上是大佬教程为你收集整理的c – std :: function:对参数进行严格的编译时验证全部内容,希望文章能够帮你解决c – std :: function:对参数进行严格的编译时验证所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。