无序的设置键是只读的,所以为什么在这种情况下我可以擦除元素:
std::unordered_set<std::string> s;
s.emplace("sth");
s.erase("sth");
Run Code Online (Sandbox Code Playgroud)
而在这不是:
std::unordered_set<std::string const> s;
const std::string str("sth");
s.emplace(str);
s.erase(str);
Run Code Online (Sandbox Code Playgroud)
如果设置本身就是const它会有意义但是使用const键我不太明白.这个断言失败了:
static_assert(!is_reference<_Tp>::value && !is_const<_Tp>::value, "");
Run Code Online (Sandbox Code Playgroud)
为什么会写那个断言的人,检查密钥是不是const?
编辑:
实际上,上面的代码编译得很好std::set.因为std::unordered_set,失败直接在实例化.重现的最小例子:
// define a customized hash ...
int main() { sizeof(std::unordered_set<int const>); }
Run Code Online (Sandbox Code Playgroud) 依赖注入的一个好处是易于测试,因为可以注入模拟类.Clazz为此目的采用原始指针并将其移动到唯一指针,以表示它拥有InjectedObj对象:
Clazz::Clazz(InjectedObj *injectedObj) : injectedObjPtr(injectedObj) { }
Run Code Online (Sandbox Code Playgroud)
injectedObjPtr会员在哪里:
std::unique_ptr<InjectedObj> injectedObjPtr;
Run Code Online (Sandbox Code Playgroud)
doSth方法通过调用智能指针调用executeSth:
//doSth method
int CLazz::doSth() {
return injectedObjPtr->executeSth();
}
Run Code Online (Sandbox Code Playgroud)
我想通过对注入对象设置一些期望来测试Clazz,所以我的测试看起来类似于:
TEST_F(testFixture, exeuteTest)
{
//before
InjectedObj* injectedObj = new InjectedObj();
EXPECT_CALL(*injectedObj, executeSth())
.Times(1)
.WillOnce(Return(100));
//when
Clazz clazz(injectedObj);
//then
ASSERT_DOUBLE_EQ(clazz->doSth(), 100);
}
Run Code Online (Sandbox Code Playgroud)
所以在这个简化的场景clazz->doSth()中调用injectedObj->executeSth哪个应该返回100,但它的行为就像我永远不会设置我的期望并且总是得到0.显然如果我将调用我的模拟对象没有智能指针:injectedObj->executeSth它返回100,但不是在使用unique_ptr调用它时.当模拟对象由智能指针管理时,有没有办法告诉gmock设置正确的期望?谢谢
我的代码剪断如下:
#include <iostream>
#include <thread>
class Me{
public:
bool isLearning;
void operator()(bool startLearning){
isLearning = startLearning;
}
};
int main(){
Me m;
std::thread t1(m(true));
t1.join();
std::cout << m.isLearning << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
传递参数时,我无法使用可调用对象启动线程,有没有办法在线程构造函数中启动线程并使用参数传递可调用对象?
为什么会导致未定义的行为?
#include <iostream>
#include <thread>
#include <vector>
std::vector<std::thread> threads(3);
void task() { std::cout<<"Alive\n";}
void spawn() {
for(int i=0; i<threads.size(); ++i)
//threads[i] = std::thread(task);
threads.emplace_back(std::thread(task));
for(int i=0; i<threads.size(); ++i)
threads[i].join();
}
int main() {
spawn();
}
Run Code Online (Sandbox Code Playgroud)
如果我将创建线程,如在注释行中线程被复制/移动赋值所以它很好,但为什么在创建线程时不起作用?
如果在赋值的rhs上抛出异常,则在下面的表达式中:bad_alloc将linesArg已经减少了吗?:
try{
buffer[--linesArg] = new char[rows];
}catch(std::bad_alloc& ba){
}
Run Code Online (Sandbox Code Playgroud) 此代码的计算结果为true:
#include <iostream>
int main(){
int x = 9;
int j = x-1;
if(x - j+1 > 1)
std::cout << "Ehhhh???\n";
}
Run Code Online (Sandbox Code Playgroud)
但这一个是假的:
#include <iostream>
int main(){
int x = 9;
int j = x-1;
if(x - (j+1) > 1)
std::cout << "Ehhhh???\n";
}
Run Code Online (Sandbox Code Playgroud)
加号和减号运算符的优先级高于"<",我也只使用一种数据类型,因此应该有bo溢出.为什么结果不同?
我有一个包含十六进制内容的文件:db90 3031 46,它应该在 vim 中显示为“\xdb\x90”,后跟“01F”,但我注意到它永远不会正确显示。然后我注意到在其他地方也是一样的,比如在终端和浏览器中我总是得到 \xdb\x9001F?这是为什么?只需将其粘贴到 google 中并尝试一下,您将永远无法将“\xdb\x90”和 0 作为下一个字符。
\n