如何使用 BOOST 二进制函数概念?
How do I use BOOST BinaryFunction concept?
我找不到任何文档,也没有我在谷歌搜索时看到的任何相关内容:
我有函数,我想产生以下签名:
(void)(int,int)
因此,当我运行这两个函数时:
void dosomething(int x, int y);
void dosomethingwront(float x, float y);
通过:
// Should succeed
boost::BinaryFunction<dosomething,void,int,int>
// Should fail
boost::BinaryFunction<dosomethingwrong,void,int,int>
编译失败,因为它不喜欢第一个参数类型。不幸的是,我不确定他们在文档中<class Func,...>
是什么意思。如果我有这两个功能,我该如何测试这些概念?
谢谢
模板参数必须是类型。 您可以使用decltype((dosomething))
. 请注意,您的概念不会失败dosomethingwront
因为int
可转换为float
并且概念检查二进制函数是否可以使用int
调用,而不是检查函数签名。
#include <boost/concept_check.hpp>
void dosomething(int x, int y);
void dosomethingwront(float x, float y);
int main() {
BOOST_CONCEPT_ASSERT((boost::BinaryFunction<decltype((dosomething)),void,int,int>));
BOOST_CONCEPT_ASSERT((boost::BinaryFunction<decltype((dosomethingwront)),void,int,int>));
}
如果要在函数参数的基础上严格检查此概念,则可以使用基于显式模板专用化的类型特征来实现。 以下内容并未穷尽所有可能的常量和易失性限定条件。
#include <iostream>
#include <type_traits>
template <typename F>
struct is_void_int_int : std::false_type {};
// Free function
template <>
struct is_void_int_int<void(int, int)> : std::true_type {};
// Pointer to function
template <>
struct is_void_int_int<void (*)(int, int)> : std::true_type {};
// Reference to function
template <>
struct is_void_int_int<void (&)(int, int)> : std::true_type {};
// Pointer to member function
template <typename C>
struct is_void_int_int<void (C::*)(int, int)> : std::true_type {};
void dosomething(int x, int y);
void dosomethingwront(float x, float y);
struct A {
void operator()(int, int) {}
};
struct B {
void bar(int, int) {}
};
int main() {
static_assert(is_void_int_int<decltype(dosomething)>::value, "!");
static_assert(is_void_int_int<decltype((dosomething))>::value, "!");
static_assert(is_void_int_int<decltype(&dosomething)>::value, "!");
static_assert(is_void_int_int<decltype(&A::operator())>::value, "!");
static_assert(is_void_int_int<decltype(&B::bar)>::value, "!");
//static_assert(is_void_int_int<decltype(dosomethingwront)>::value, "!"); // BOOM!
}
相关文章:
- 多态二进制函数
- 二进制搜索树没有匹配的函数调用
- 为二进制树编码remove()函数
- 如何使用 BOOST 二进制函数概念?
- 在Qt中使用C++Lambda函数作为插槽是否有助于保持库的二进制兼容性?
- 模板函数中的无捕获 lambda 是否具有多个二进制实现?
- 如何将Z3_ast导出为二进制以及如何搜索函数名称?
- 查找二进制文件的大小,函数 tellg() 返回 -1
- 类型长的长度和未解决的超载函数类型的类型的操作数与二进制运算符
- 类没有合适的复制构造函数,找不到二进制运算符 '=',
- Rcpp - 用二进制函数转换数字向量?
- 没有变量声明为函数,但错误:二进制表达式的操作数无效
- 为什么新的虚拟函数不会破坏二进制兼容性的现象?
- C++中的类/函数模板占用了我的二进制文件的百分比是多少?
- 错误:二进制表达式的操作数无效(映射使用查找函数错误)
- <未解析的重载函数类型>调用二进制谓词
- 此TreeVisitor函数如何用于此二进制搜索树分配
- 在 C++ 中使用按位运算符理解十进制到二进制函数的问题
- 使用二进制检测挂钩函数
- 从构造函数二进制中删除"显式"是否兼容?