我正在使用Boost Test来测试一些C++代码.
我有一个值的向量,我需要与预期的结果进行比较,但我不想手动检查循环中的值:
BOOST_REQUIRE_EQUAL(values.size(), expected.size());
for( int i = 0; i < size; ++i )
{
BOOST_CHECK_EQUAL(values[i], expected[i]);
}
Run Code Online (Sandbox Code Playgroud)
主要问题是循环检查不会打印索引,因此需要进行一些搜索才能找到不匹配.
我可以使用std::equal
或std::mismatch
在两个向量上,但这也需要很多样板.
有更清洁的方法吗?
我试图让我的程序在没有boost
使用的情况下工作,但是找不到某些有用模式的替代方案.也就是说,我boost::optional
在标准库中找不到-likewise模式.是否有boost::optional
(C++ 11或其他地方)的标准替代方案?
我想绘制一个表,其中最后一列应位于表的最右侧.
这是表行的样子:
Admin (2)New
Network (2)New
Run Code Online (Sandbox Code Playgroud)
这应该是这样的:
Admin (2) New
Network (2) New
Run Code Online (Sandbox Code Playgroud)
XML:
<?xml version="1.0" encoding="utf-8"?>
<!-- row.xml -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="45dp"
android:gravity="center" android:background="@color/list_bg">
<TableLayout android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow>
<ImageView android:id="@+id/t1" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:id="@+id/t2" android:typeface="normal"
android:singleLine="true" android:textSize="14sp" android:textStyle="normal"
android:layout_width="wrap_content" android:textColor="#000000"
android:layout_height="wrap_content" />
<TextView android:id="@+id/t10" android:typeface="normal"
android:singleLine="true" android:text=" " android:textSize="14sp"
android:textStyle="normal" android:layout_width="wrap_content"
android:textColor="#000000" android:layout_height="wrap_content" />
<TextView android:id="@+id/t4" android:typeface="normal"
android:visibility="gone" android:singleLine="true" android:text="("
android:textSize="14sp" android:textStyle="normal"
android:layout_width="wrap_content" android:textColor="#000000"
android:layout_height="wrap_content" />
<TextView android:id="@+id/t5" android:typeface="normal"
android:visibility="gone" android:singleLine="true"
android:textSize="14sp" android:textStyle="normal"
android:layout_width="wrap_content" android:textColor="#000000"
android:layout_height="wrap_content" /> …
Run Code Online (Sandbox Code Playgroud) 我刚学会了C++ 17中保证的复制省略.根据该问题的答案:
执行
return T();
此操作时,将通过a初始化函数的返回值prvalue
.由于该函数返回T,因此不会创建临时函数;prvalue
简单的初始化直接初始化返回值.要理解的是,由于返回值是a
prvalue
,它还不是一个对象.它只是一个对象的初始化器,就像T()
是.
所以我想知道,这是否适用于除以下之外的任何事情:
T f() {return T();}
T t = f();
Run Code Online (Sandbox Code Playgroud)
所以我编写了这段代码emplace_back
来测试它:
#include <vector>
#include <iostream>
struct BigObj{
BigObj() = default;
BigObj(int) { std::cout << "int ctor called" << std::endl; }
BigObj(const BigObj&){
std::cout << "copy ctor called" << std::endl;
}
BigObj(BigObj&&){
std::cout << "move ctor called" << std::endl;
}
};
BigObj f(){ return BigObj(2); }
int g(){ return 2; }
int main(){ …
Run Code Online (Sandbox Code Playgroud) 类型擦除 - 你怎么称呼它?
如何boost::shared_ptr
存储其删除器以及如何boost::function
存储其功能对象?
有没有教授这个技巧的教程?
使用类型擦除函数对象的运行时成本是多少?
在Stackoverflow上,有很多关于从a-priory未知范围生成均匀分布的整数的问题.例如
典型的解决方案是这样的:
inline std::mt19937 &engine()
{
thread_local std::mt19937 eng;
return eng;
}
int get_int_from_range(int from, int to)
{
std::uniform_int_distribution<int> dist(from, to);
return dist(engine());
}
Run Code Online (Sandbox Code Playgroud)
鉴于分布应该是一个轻量级对象并且没有性能问题需要多次重新创建它,看起来即使是简单的分发也可能很好并且通常会有一些内部状态.
所以我想知道是否通过不断重置它来干扰分布如何工作(即在每次调用时重新创建分布get_int_from_range
)我得到了正确分布的结果.
Pete Becker和Steve Jessop之间进行了长时间的讨论,但没有最后的说法.在另一个问题中(我应该保留随机分布对象实例还是可以随时重新创建它?)内部状态的"问题"似乎并不重要.
C++标准是否对此主题做出了任何保证?
以下实现(来自N4316 - std :: rand替换)是否更可靠?
int get_int_from_range(int from, int to)
{
using distribution_type = std::uniform_int_distribution<int>;
using param_type = typename distribution_type::param_type;
thread_local std::uniform_int_distribution<int> dist;
return dist(engine(), param_type(from, to));
}
Run Code Online (Sandbox Code Playgroud)
编辑
这重用了分发的可能的内部状态,但它很复杂,我不确定它是否值得麻烦:
int get_int_from_range(int from, int to)
{
using …
Run Code Online (Sandbox Code Playgroud) 我已经多次被告知,我应该使用参数std::async
来解决火灾和遗忘类型的任务std::launch::async
(所以它最好在新的执行线程上使用它).
在这些陈述的鼓励下,我想看看如何std::async
比较:
std::thread
我天真的异步实现看起来像这样:
template <typename F, typename... Args>
auto myAsync(F&& f, Args&&... args) -> std::future<decltype(f(args...))>
{
std::packaged_task<decltype(f(args...))()> task(std::bind(std::forward<F>(f), std::forward<Args>(args)...));
auto future = task.get_future();
std::thread thread(std::move(task));
thread.detach();
return future;
}
Run Code Online (Sandbox Code Playgroud)
没有看中这里,包函子f
成std::packaged task
与它的参数一起,启动它的一个新的std::thread
被分离,并用返回std::future
的任务.
现在代码测量执行时间std::chrono::high_resolution_clock
:
int main(void)
{
constexpr unsigned short TIMES = 1000;
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < TIMES; ++i)
{
someTask();
}
auto dur = …
Run Code Online (Sandbox Code Playgroud) 我正在编写一个具有类似矩阵结构的类,我希望有一个名为minor的成员函数与矩阵操作相同.这会触发一些错误.我系统上的最小测试用例:
#include <iterator>
void minor(int row, int col);
Run Code Online (Sandbox Code Playgroud)
编译时,clang提供以下错误:
$ clang++ -Weverything -std=c++11 test.cpp
test.cpp:2:21: error: too many arguments provided to function-like macro invocation
void minor(int row, int col);
^
/usr/include/x86_64-linux-gnu/sys/sysmacros.h:67:10: note: macro 'minor' defined here
# define minor(dev) gnu_dev_minor (dev)
^
test.cpp:2:6: error: variable has incomplete type 'void'
void minor(int row, int col);
^
2 errors generated.
$
Run Code Online (Sandbox Code Playgroud)
sys/sysmacros.h的相关部分是:
/* Access the functions with their traditional names. */
# define major(dev) gnu_dev_major (dev)
# define minor(dev) gnu_dev_minor (dev)
# …
Run Code Online (Sandbox Code Playgroud) 在被认为有害的rand()中,它被指出srand(time(NULL))
是坏的,因为srand
需要一个unsigned int
,但对于Microsoft的编译器,time_t
默认情况下是64位数,因此发生缩小转换.但是,time_t
是实现定义的.
既然我看到srand(time(NULL))
如此普遍(即使在这个网站上),是否应该气馁?
c++ ×8
c++11 ×4
boost ×3
random ×2
android ×1
asynchronous ×1
c++17 ×1
distribution ×1
emplace ×1
glibc ×1
libstdc++ ×1
macros ×1
prng ×1
srand ×1
std ×1
stdasync ×1
type-erasure ×1
unit-testing ×1