- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
受到 Haskell 自动生成(随机)给定类型实例的优雅方式的启发,例如在 QuickCheck 中,我正在尝试弄清楚如何编写一个 as-easy-to-use-as-可能的 C++ 基准测试框架。我想我将使用函数模板,可能会借助 C++11 中的新功能,例如可变参数模板。我希望我只需要指定一个函数或更好的函数模板和一个与函数的参数兼容的 STL 模板容器类型(反过来它的 value_type
)。
我认为,使用一组不同大小的输入对函数进行基准测试有点类似于 C++11 中线程的设置和生成方式。我的第一个尝试是复制 thread
类的构造函数并将其转换为 benchmark
函数作为
template< class Function, class ...Args >
inline
void benchmark( Function&& f, Args&&... args );
我不确定我们是否应该在这里使用右值引用。但是,f
和 args
必须在调用 benchmark
之前显式实例化,导致 无法正常使用。
这导致我尝试跳过调用参数而只使用模板参数:
namespace pnw
{
template <template <typename> class Function, typename Container>
inline
void benchmark_container()
{
Function<typename Container::iterator> f;
Container c(10);
f(c.begin(), c.end());
}
}
称为
typedef int T;
typedef std::vector<T> C;
pnw::benchmark_container<std::sort, C>();
但是,现在编译错误为
tests/t_histogram.cpp: In function ‘void test_benchmark()’:
tests/t_histogram.cpp:56:44: error: no matching function for call to ‘benchmark_container()’
tests/t_histogram.cpp:56:44: note: candidate is:
tests/../benchmark.hpp:32:6: note: template<template<class> class Function, class Container> void pnw::benchmark_container()
我不确定 C++ 是否可以仅通过另一个调用函数的模板参数来处理传递函数模板。
这是正确的方法还是在 C++11 中不可能?我正在使用 GCC-4.6。
最佳答案
如果你需要支持“higher-kinded”参数,你必须使用template-template parameters .此外,在模板内,如果 typename
未限定,f::g
将被视为一个值。因此你应该写:
template <template <typename> class Function, typename Container> // <--
inline void benchmark_container()
{
Function<typename Container::iterator> f; // <--
...
(所有这些都在 C++11 之前可用。)
编辑但是电话
benchmark_container<std::sort, C>();
不会工作,因为 std::sort
是一个重载的模板函数,而不是类模板。您也不能单独引用 std::sort
,因为它会产生歧义。
如果你只想使用像 std::sort
这样没有关联上下文的函数,你可以传递一个函数指针来消除重载的歧义:
template <typename Container,
void (*func)(typename Container::iterator, typename Container::iterator)>
inline void benchmark_container()
{
Container c (10);
func(c.begin(), c.end());
}
benchmark_container<std::vector<int>, std::sort>();
或
template <typename Container>
inline void benchmark_container(void (*func)(typename Container::iterator, typename Container::iterator))
{
Container c (10);
func(c.begin(), c.end());
}
benchmark_container<std::vector<int>>(std::sort);
或者只是手动选择你想使用的重载,允许传递通用函数对象:
template <typename Container, typename F>
inline void benchmark_container(const F& function)
{
Container c (10);
function(c.begin(), c.end());
}
benchmark_container<std::vector<int>>(std::sort<std::vector<int>::iterator>);
关于c++ - C++ 中类似 QuickCheck 的模板函数基准测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8895350/
我正在处理 Real WorldHaskell其中一个 第4章的练习是实现一个foldr基于版本的concat .我认为这将是一个很好的测试候选者 QuickCheck 因为有一个现有的实现来验证我的
我要延期 QuickCheck在测试失败时给我更好的信息(而不仅仅是种子)。例如,我希望能够按照以下方式创建内容: eqTest :: Eq a => a -> a -> TestResult eqT
有没有人确切地知道如何使用 QuickCheck 在 Haskell 中定义生成器,以便仅选择一次所选元素? 我已经意识到我可能需要一个“Gen(Maybe Positive)”生成器,但这当然会产生
我有以下要使用快速检查测试的属性: prop_zip xs ys = length xs == length ys ==> unzip (zip xs ys) == (xs,ys) 尽管根据
我有一个具有以下类型签名的函数 rndListIndex :: Double -> Double -> Double -> Double rndListIndex maxIdx r1 r2 = … 第
鉴于以下情况: test :: (Int -> Int) -> Int -> Bool test _ _ = True 编译源码后,我尝试运行quickCheck test : > quickChec
为什么没有QuickCheck功能类似于 hedgehog的 success ?特别是我想知道如何翻译如下属性: prop_specialPair :: Property prop_specialPa
我有一个如下所示的 QuickCheck 属性: prop42 :: Foo -> Bool prop42 foo = fn1 foo == fn2 foo 如果此属性失败,它将打印出 foo曾是。但
所以我有这个 quickCheck 命题逻辑生成器: instance Arbitrary Form where arbitrary = genForm genForm = sized genF
背景 为了好玩,我正在尝试编写一个用于快速检查的属性,以测试 cryptography with RSA 背后的基本思想。 . 选择两个不同的素数,p和 q . 让 N = p*q e是某个数字rel
我有一组对字符串进行编码/解码的简单演示程序,并希望为它们生成一些 quickCheck 测试,但将测试限制为仅可打印的字符串。由于生成和拒绝的测试用例太多,使用 guard 太慢并且失败,所以我想为
我有两个生成器,gen_n 和 gen_arr: gen_n :: Gen Int gen_n = suchThat arbitrary (\i -> i >= 0 && i i >= 0 && i
我面临着为生成器编写收缩函数的问题,该函数取决于另一个生成器输出的值。基本上是以下形式的生成器: do a Gen b g :: a -> b -> c 。为了便于论证,假设 g = (,) 。
我有一个数据类型定义: data Point = Point {x :: Int, h :: Int} | EmptyPoint 在我的属性测试中,我想将测试限制为仅针对 Point 构造函数案例。例
propertyForStringsFromMyCharPool :: String -> Bool -- implementation main = T.quickCheck propertyFor
我有一个类型类 Atomic,它定义了将某些类型与包装器值 (Atom) 相互转换的函数。我想定义一个 QuickCheck 属性,它声明:“对于 Atomic 的所有实例,可以安全地存储和检索任何值
tl;dr:如果您的数据类型允许太多嵌套,您如何编写不会爆炸的 Arbitrary 实例?您如何保证这些实例产生您的数据结构的真正随机样本? 我想生成随机树结构,然后在用我的库代码破坏这些结构后测试它
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题吗? 更新问题,以便 editing this post 提供事实和引用来回答它. 关闭 7 年前。 Improve
我正在尝试为身份编写一个 QuickCheck 测试 f $ y = f y 我最初的计划是编写一个返回函数和整数的任意生成器,签名为 Gen (Int -> Int, Int) 并且在 prop_D
我正在使用 Test.QuickCheck 生成随机数独谜题。 data Sudoku = Sudoku { getSudoku :: [[Maybe Int]] } deriving (Show,
我是一名优秀的程序员,十分优秀!