草庐IT

std-ranges

全部标签

c++ - 由于类型不完整,在 static_assert 中使用 std::is_base_of 失败

我想做的是让一些类继承自extention类。问题是extention类必须知道它正在扩展哪个类。这可以像这样简单地实现:templateclassExtention{public:voidcheck()const{std::cout::value{};classBar:publicExtention{};Foo和Bar类显示了扩展的好坏用法。Foo().check();→Extentionisvalid:trueBar().check();→Extentionisvalid:false我想在编译时检查模板的有效性,这让我写了templateclassExtention{static_

c++ - std::hash 用于 std::chrono::duration

我正在尝试在std::chrono::duration上设置一组对象的键。这不会编译:#include#includeclassFoofinal{public:Foo(){}inty;};intmain(void){automap=std::unordered_map,Foo>();map[std::chrono::duration(5)].y=0;return0;}/usr/include/c++/4.9/bits/hashtable_policy.h:Ininstantiationof'structstd::__detail::__is_noexcept_hash>,std::ha

c++ - std::experimental::ostream_joiner 和 std::pair

在c++17/g++7中,终于有了怀念已久的ostream_joiner。它可以正确输出到ostream,使用中缀定界符分隔集合元素。#include#include#include#include#include#includeusingstring=std::string;#if1structpair{stringfirst;stringsecond;};#elseusingpair=std::pair;#endifstd::ostream&operatorpairs={{"foo","bar"},{"baz","42"}};std::copy(std::begin(pairs),

c++ - 从 vector 映射中查找并删除 std::function 对象

我目前正在尝试为我的游戏引擎实现一个消息系统。它使用以下形式的函数回调:typedefstd::functionCallback;它维护一个消息列表:mutablestd::vector>messageList;以及签名的回调字典:mutablestd::map>callbackDictionary;用于调用“绑定(bind)”到特定消息类型的所有回调。调用回调函数时,将传递相应的消息。到目前为止一切顺利。为了更好地理解,这里是订阅方法,它允许用户添加一个函数方法,该方法会为订阅类型的每条消息调用。voidMessenger::Subscribe(Message::Typetype,C

c++ - std::iterator_traits libstdc++ 和 libc++ 之间的分歧

给定:structIter{usingvalue_type=int;usingdifference_type=int;usingreference=int;usingpointer=int;usingiterator_category=int;};以下代码适用于libstc++,但无法针对libc++5.0.0进行编译:#include#includestatic_assert(std::is_same::iterator_category,Iter::iterator_category>::value,"");出现错误:error:nomembernamed'iterator_cat

c++ - 遗留 c++ 代码不包含 std::prefix

我正在使用g++4.4.7编译一段非常古老的遗留代码。关于这段代码,我真正知道的是它是在Irix/Sun系统上开发的,这意味着它具有MIPS体系结构。我在使用这段代码时发现的一件相当奇怪的事情是,它有时会调用像endl和set_new_handler这样的函数而没有std::前缀。显然,这会导致编译错误。由于我假设这段代码有时会在某台机器上编译,因此我对盲目添加std::前缀以使其编译有点谨慎,因为它可能会改变行为.那么,是否有一些旧的非ISO编译器允许这段代码编译?或者是否有某种标志可以传递给gcc以允许这段代码工作? 最佳答案

c++ - 从 std::aligned_union_t 获取指向包含对象的指针

我想使用placement-new在std::aligned_union_t中构造一个任意类型的对象。一旦构造成功,我希望能够取回指向构造对象的指针,而不用单独存储它。通过简单地reinterpret_cast'ingstd::aligned_union_t这样做是否合法,只要我确保将其转换为构造的原始类型?下面的示例代码是否合法?MyStruct是否应该满足任何类型特征要求?例如,它必须是POD吗?#include#include#include#includestructMyStruct{intvalue=0;};constexprsize_tc_alignedUnionSize=

c++ - std::allocate_shared,允许从自定义分配器和单次分配中分配共享指针引用计数

Makeshared使用引用计数和对象的单一分配提高性能,是否可以将自定义分配器与std::allocate_shared一起使用并且仍然有一个分配,根据我写的测试代码它没有发生:sample我知道boost::intrusive,但它有额外的代码需要编写并且可能出错 最佳答案 在一些调试的帮助下发现std::allocate_shared做了预期的事情,它允许你只为你的对象和引用计数器进行一次分配。下面是更正后的代码:allocateshared此处operatornew对std::make_shared和std::allocat

c++ - std::string_view 在传递给另一个采用 std::string 的函数时的优点

我有以下辅助函数,它接受一个字符串View并在unordered_map中返回一个查找:intScanner::getOpCount(std::string_viewop){autoitr=Parser::opTable.find(op);}//inanotherfile:conststaticstd::unordered_mapopTable;这不会编译,因为find需要一个字符串参数,所以我找到的唯一解决方案(如果我错了请纠正我)是将op包装为字符串{操作}。然而,让我担心的是std::string_view是为了简化字符串传递,但是如果我必须从它构造一个字符串而不考虑函数体,那么

c++ - 多次调用 `std::future::then` 的行为是什么?

根据ConcurrencyTS,下面的代码会发生什么?autof0=std::async([]{return0;});autof1=f0.then([](auto&f){returnf.get()+10;});autof2=f0.then([](auto&f){if(!f.valid())return;returnf.get()+10;});到第三行代码执行时,f0已经有了continuation,所以根据TS,f0应该抛出异常,中止程序,UB或者有不同的行为?我不清楚。 最佳答案 根据cppreference,它是未定义的:Att