草庐IT

assert_equal

全部标签

Java——list.stream().filter(item -> item.getType().equals(“type”)).findFirst()报空指针

可能原因1.list对象为null2.item对象为null3.type对象为null在Java中使用list.stream().filter(item->item.getType().equals(type)).findFirst()方法链时,出现空指针异常(NullPointerException)的原因可能是:1.list对象为null检查list是否已经正确初始化,确保其不为null。如果list为null,调用stream()方法时会导致空指针异常。2.item对象为null在Lambda表达式中调用item.getType()时,item可能为null。在调用方法之前,你应该确保i

c++ - 检查: equal to or not equal to?有什么效率

我想知道,如果我们有if-else条件,那么检查什么在计算上更有效:使用等于运算符或不等于给运营商?有什么区别吗?例如,以下哪一项在计算上是高效的,下面的两种情况都会做同样的事情,但哪一种更好(如果有任何区别)?案例一:if(a==x){//executeSet1ofstatements}else{//executeSet2ofstatements}案例2:if(a!=x){//executeSet2ofstatements}else{//executeSet1ofstatements}此处假设大多数情况下(比如90%的情况)a将等于x。a和x都是无符号整数类型。

c++ - pthread_mutex_lock.c :62: __pthread_mutex_lock: Assertion `mutex->__data.__owner == 0' failed

我收到了那个错误:pthread_mutex_lock.c:62:__pthread_mutex_lock:Assertion`mutex->_data._owner==0'failed.而且我找不到任何原因。但是我不确定以下代码:声明:std::mutexlock;std::condition_variablecond;锁定和解锁的顺序:std::unique_locklk(lock);cond.wait(lk);lock.unlock();如果我删除这个序列-一切正常,但没有任何保护。我不确定我是否正确使用了unique_lock。 最佳答案

c++ - 如何 static_assert 初始化列表是一定大小

是否可以验证传递给constexpr构造函数的初始化列表是否具有特定大小?或者这只能在运行时执行?这是想法,但行不通:structgroup{constexprgroup(std::initializer_listconst>groups){static_assert(each_list_size_greater_than_1(groups.begin(),groups.end()));}constexprstaticbooleach_list_size_greater_than_1(std::initializer_listconst>::const_iteratorconstbeg

c++ - for语句的for-init-statement中的static_assert

是什么阻止编译器编译放置在for语句的for-init-statement中的static_assert?例如:for(static_assert(true,"");false;);//error 最佳答案 Grammatically.for循环是:for(init-statement;conditionopt;expressionopt)statement其中init-statement可以是expression后跟;或simple-declaration。static_assert-declaration不是那些东西,因此它不能

C++ std::equal —— 不测试大小相等的 2 个范围的理由是什么?

我刚刚写了一些代码来测试std::equal的行为,结果很惊讶:intmain(){try{std::listlst1;std::listlst2;if(!std::equal(lst1.begin(),lst1.end(),lst2.begin()))throwstd::logic_error("Error:2emptylistsshouldalwaysbeequal");lst2.push_back(5);if(std::equal(lst1.begin(),lst1.end(),lst2.begin()))throwstd::logic_error("Error:comparin

c++ - 是否可以为不应编译的表达式表达 static_assert?

我想用以下形式表达一个static_assert:static_assert(expressionshouldnotcompile);让我添加一个完整的例子:templatestructA{};templatestructA{voida(){}};Ab;static_assert(!compile(b.a()));orstatic_assert(!compile(A::a()));因此,我们的想法是能够确保表达式(具有有效语法)不会被编译。如果可能的话,如果解决方案只使用C++11会更好。 最佳答案 好的,考虑到您问题的上下文有些模

c++ - has_equal_operator 在 C++11 中的实现

我正在尝试实现has_equal_operator在C++11中,到目前为止提出了以下解决方案。它适用于像int这样的简单情况或structA{}但对于std::vector失败(返回误报).为什么会失败以及如何解决这个问题?#include#includetemplateconstexprautohas_equal_operator(int)->decltype(std::declval()==std::declval(),bool()){returntrue;}templateconstexprboolhas_equal_operator(...){returnfalse;}str

c++ - 测试用例 VS ASSERTION 语句

在我的大多数C++项目中,我大量使用如下ASSERTION语句:intdoWonderfulThings(constint*fantasticData){ASSERT(fantasticData);if(!fantasticData)return-1;//,,,returnWOW_VALUE;}但是TDD社区似乎喜欢做这样的事情:intdoMoreWonderfulThings(constint*fantasticData){if(!fantasticData)returnERROR_VALUE;//...returnAHA_VALUE;}TEST(TDD_Enjoy){ASSERT_

c++ - 通过 decltype 表达式调用时 static_assert 是否应该工作?

我预计以下代码会因最后一行的static_assert检查而失败。但是在MSVC2015和gcc6.2中,它编译成功。它确实无法按预期在clang3.9中进行编译。这是编译器错误还是static_assert在decltype()中不起作用?#include#includetemplatestructWrapper{};templateconstexprstd::tupleoperator|(Wrapper,Wrapper){static_assert(std::is_same::value==false,"can'tcombinetwoofthesametype");returnst