本文整理汇总了C++中Optional::clear方法的典型用法代码示例。如果您正苦于以下问题:C++ Optional::clear方法的具体用法?C++ Optional::clear怎么用?C++ Optional::clear使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Optional
的用法示例。
在下文中一共展示了Optional::clear方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: int
TEST(Optional, Shared) {
shared_ptr<int> ptr;
Optional<shared_ptr<int>> opt;
EXPECT_FALSE(bool(opt));
// empty->emplaced
opt.emplace(new int(5));
EXPECT_TRUE(bool(opt));
ptr = opt.value();
EXPECT_EQ(ptr.get(), opt->get());
EXPECT_EQ(2, ptr.use_count());
opt.clear();
EXPECT_EQ(1, ptr.use_count());
// full->copied
opt = ptr;
EXPECT_EQ(2, ptr.use_count());
EXPECT_EQ(ptr.get(), opt->get());
opt.clear();
EXPECT_EQ(1, ptr.use_count());
// full->moved
opt = std::move(ptr);
EXPECT_EQ(1, opt->use_count());
EXPECT_EQ(nullptr, ptr.get());
{
Optional<shared_ptr<int>> copied(opt);
EXPECT_EQ(2, opt->use_count());
Optional<shared_ptr<int>> moved(std::move(opt));
EXPECT_EQ(2, moved->use_count());
moved.emplace(new int(6));
EXPECT_EQ(1, moved->use_count());
copied = moved;
EXPECT_EQ(2, moved->use_count());
}
}
示例2:
TEST(Optional, NoDefault) {
Optional<NoDefault> x;
EXPECT_FALSE(x);
x.emplace(4, 5);
EXPECT_TRUE(bool(x));
x.clear();
EXPECT_FALSE(x);
}
示例3:
TEST_F(ScopedEventBaseThreadTest, eb_dtor_in_io_thread) {
Optional<ScopedEventBaseThread> sebt;
sebt.emplace();
auto const io_thread_id = sebt->getThreadId();
EXPECT_NE(this_thread::get_id(), io_thread_id) << "sanity";
auto const eb = sebt->getEventBase();
thread::id eb_dtor_thread_id;
eb->runOnDestruction(new EventBase::FunctionLoopCallback(
[&] { eb_dtor_thread_id = this_thread::get_id(); }));
sebt.clear();
EXPECT_EQ(io_thread_id, eb_dtor_thread_id);
}
示例4:
template<typename T> void _readPropertyOfElement(
xercesc::DOMElement *parent,
const char *namespaceURI,
const char *localName,
Optional<T>& field,
void(*xmladapter)(const char *, Optional<T>&) = &xmlAdapter) {
const XMLCh *text = DOMHelper::getElementTextContentByTagNameNS(
parent,
DOMHelper::fromUTF8(XML_NS),
DOMHelper::fromUTF8(localName));
if (text) {
xmladapter(DOMHelper::toUTF8(text), field);
} else {
field.clear();
}
}
示例5: opt
TEST(Optional, Const) {
{ // default construct
Optional<const int> opt;
EXPECT_FALSE(bool(opt));
opt.emplace(4);
EXPECT_EQ(*opt, 4);
opt.emplace(5);
EXPECT_EQ(*opt, 5);
opt.clear();
EXPECT_FALSE(bool(opt));
}
{ // copy-constructed
const int x = 6;
Optional<const int> opt(x);
EXPECT_EQ(*opt, 6);
}
{ // move-constructed
const int x = 7;
Optional<const int> opt(std::move(x));
EXPECT_EQ(*opt, 7);
}
// no assignment allowed
}