草庐IT

forward_static_call

全部标签

c++ - forward_list、set、list 等如何调用 std::allocator?

我注意到分配器只能分配T类型的对象并保留大小为n*sizeof(T)的内存块.std::list内部的链表节点然而,类型不一定是T类型的对象,它们的大小也不一定与T相同对象。那样的话,怎么可能std::list使用std::allocator分配内存? 最佳答案 这就是为什么rebindtype存在。它允许您创建一个类似的分配器,而不是分配其他东西(例如node)。基本上是这样的:std::allocatorint_alloc;std::allocator::rebind>node_alloc;//Perhapsmoreuseful

c++ - 组合 std::forward、std::move 和 volatile 时的意外返回类型

代码ongcc.godbolt.org.我创建了一个简单的类型特征来删除右值引用:templatestructremove_rvalue_reference{usingtype=T;};templatestructremove_rvalue_reference{usingtype=T;};templateusingremove_rvalue_reference_t=typenameremove_rvalue_reference::type;我用它来实现一个copy_if_rvalue(x)函数,其返回类型取决于传递的参数:templateconstexprautocopy_if_rva

c++ - SFINAE : Detecting if a function is called by a compile time known value

我喜欢在我的一个ctors以编译时已知值被调用时做一些检查。有办法检测吗?所以当有人调用它时:Aa(10);因为10是编译时已知常量,所以我喜欢调用一个特殊的构造函数,如下所示:template>A(intValue){}知道如何解决这个问题吗?谢谢! 最佳答案 积分常量可以解决您的问题:structA{template*=nullptr>A(std::integral_constant){}};然后,你可以像这样使用它:Aa{std:integral_constant{}};为了便于使用,您还可以使用类似于boost::hana的

c++ - std::forward_list::remove_if 谓词的要求

考虑这段代码:structT{boolstatus;UsefulDatadata;};std::forward_listlst;lst.remove_if([](T&x)->bool{returnx.status=!x.status;});即一次性切换状态和删除非事件元素。根据cppreference上面的代码似乎是未定义的行为(强调我的):templatevoidremove_if(UnaryPredicatep);p-unarypredicatewhichreturnstrueiftheelementshouldberemoved.Thesignatureofthepredicat

c++ - 关于多线程环境下static const变量的使用

我正在尝试了解潜在的场景以及它是否可能成为问题。所以我有一个当前线程安全的静态函数。函数是这样的:staticthread_safe_func(){...process}现在在此函数中,我添加以下内容:staticthread_safe_func(){staticconstClass::NonThreadSafeClassName()*array[16]={Class::NonThreadSafeClassName(),Class::NonThreadSafeClassName(),Class::NonThreadSafeClassName(),Class::NonThreadSafe

c++ - 如何使用 std::forward_list 在恒定时间内进行范围拼接?

我想拼接范围[first,last],包括两个端点。我有元素beforefirst和last的迭代器。我可以使用splice_after()来完成,但只能在线性时间内完成。我相信这个拼接可以在恒定时间内完成。我如何使用std::forward_list完成它?如果问题不清楚,这里是显示我的问题的示例代码:LiveWorkSpace上的代码#include#include#include#includeusingnamespacestd;intmain(){forward_listtrg{'a','b','c'};forward_listsrc{'1','2','3','4'};auto

c++ - 错误 : invalid use of member in static member function

我有两个类,这是其中一个的标题:#ifndefWRAPPER_HPP#defineWRAPPER_HPP#includeusingnamespacestd;classWrapper{private://SDL_Surface*screen;public:staticSDL_Surface*screen;staticvoidset_screen(SDL_Surface*_screen);staticvoidset_pixel(intx,inty,Uint8color);staticvoidclear_screen(intr,intg,intb);staticSDL_Surface*loa

c++ - 为什么在我的析构函数中抛出时总是得到 "terminate called after throwing an instance of..."?

我正在尝试编写一个单元测试来检测对我的类的lock()功能的无效使用。为此,我想使用析构函数并从那里抛出异常。不幸的是,g++没有捕获异常,而是决定调用std::terminate()。类有一个非常简化的版本:classA{public:A():f_lock(0){}~A(){if(f_lock)throwmy_exception("stilllocked");}lock(){++f_lock;}unlock(){--f_lock;}private:intf_lock;};有一个有效的测试:A*a=newA;a->lock();...a->unlock();deletea;我正在尝试编

c++ - std::move 与 std::forward

这似乎是已经提出的最相关的问题。Whatsthedifferencebetweenstd::moveandstd::forward但是每个答案都是不同的,适用的和说的东西也略有不同。所以我很困惑。我有以下情况。将项目复制到容器中Copy项是C++03,所以我非常理解。将项目构建到容器中Constructitemintocontainer我相信使用完美转发正确地通过两个函数将参数转发到emplaceBackInternal()中T的构造函数(如果我错了请指出)。将元素移入容器我的问题似乎是理解如何将元素移入容器。代码:templateclassContainer{std::size_tl

c++ - 如果函数被称为 constexpr,则有条件地 static_assert

我知道有人提议使用constexpr()运算符,但这还没有在gcc/clang中实现。我也知道有一个使用机器代码编辑等技巧的实现:http://saadahmad.ca/detecting-evaluation-context-inside-constexpr-functions/我想知道是否有一个有点受限的解决方案:structF{constexprF(intv){ifconstexpr(constexpr()){static_assert(v>0);}else{assert(v>0);}}};//...constexprFf{0};//shouldtriggeracompile-t