Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 函数:参数的严格编译时验证_C++_Templates_Gcc_C++11_Std Function - Fatal编程技术网

C++ 函数:参数的严格编译时验证

C++ 函数:参数的严格编译时验证,c++,templates,gcc,c++11,std-function,C++,Templates,Gcc,C++11,Std Function,我想实现一个类,它包含两个带有预定义函数签名的回调 该类具有模板化的ctor,它使用std::bind创建std::函数成员。我希望编译器(g++4.6)会抱怨,如果一个签名错误的函数被传递给了ctor。但是,编译器接受以下内容: callback c1(i, &test::func_a, &test::func_a); 我能理解为什么会这样。我试图为静态断言构造一个适当的条件,但没有成功 如何生成编译时错误来防止这种情况 #include <functional

我想实现一个类,它包含两个带有预定义函数签名的回调

该类具有模板化的ctor,它使用std::bind创建std::函数成员。我希望编译器(g++4.6)会抱怨,如果一个签名错误的函数被传递给了ctor。但是,编译器接受以下内容:

    callback c1(i, &test::func_a, &test::func_a);
我能理解为什么会这样。我试图为静态断言构造一个适当的条件,但没有成功

如何生成编译时错误来防止这种情况

#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, inst, _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_a, &test::func_b);

// Both should fail at compile time

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

    return 0;
}
尽管函数签名有效,但在这种情况下,此处会触发访问者建议的静态_断言:

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");
static_断言(std::is_same::value,“第一个函数类型不正确”);
静态断言(std::is_same::value,“第二个函数类型不正确”);

请参阅:

太好了!我试图使用std::is_same,但我错误地使用了类型a/type_b而不是函数签名。顺便说一句,可以从std::function typedef(DRY)推断函数的类型吗?它在这里没有多大用处,因为std::function接受任何类型的函数,只要它们可以用模板参数给定的参数类型调用即可。-关于从
类型a
中提取
bool(int&)
,是的,从技术上讲,使用元函数是可行的:
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");