草庐IT

Any-Integer

全部标签

c++ - 通用转换运算符模板和 move 语义 : any universal solution?

这是Explicitref-qualifiedconversionoperatortemplatesinaction的后续事件.我已经尝试了许多不同的选项,我在这里给出了一些结果,试图看看最终是否有任何解决方案。假设一个类(例如any)需要以一种方便、安全(毫无意外)的方式提供对任何可能类型的转换,同时保留move语义。我能想到四种不同的方法。structA{//explicitconversionoperators(nice,safe?)templateexplicitoperatorT&&()&&;templateexplicitoperatorT&()&;templateexpl

c++ - boost::any/std::any 是否就地存储小对象?

为了容纳任意大的对象,boost::any/std::any肯定需要为对象分配堆空间。但是,对于大小小于或等于指针(int,char,bool,...)的小型类型,any可以将值原地存储在指针槽或其他一些就地内存,而不分配堆空间。但是实现会这样做吗?我有一个场景,我经常将小类型存储在any中,有时只存储较大的类型,例如strings。代码很热,因此我在问这个问题。如果不执行优化,我可能会更好地使用自己的实现来就地存储小类型。 最佳答案 没有保证,但[any.class]中的C++17草案状态那个Implementationsshou

c++ - 为什么 `boost::any` 比 `void*` 好?

boost::any和boost::any_cast与使用void*和dynamic_cast相比有哪些内在优势>? 最佳答案 优点是boost::any比void*更安全。例如inti=5;void*p=&i;static_cast(p);//Compilerdoesn'tcomplain.UndefinedBehavior.boost::anya;a=i;boost::any_cast(a);//throws,whichisgood至于您的评论,您不能从void*中dynamic_cast。您可以dynamic_cast仅从具有

c++ - 我是否需要同步 std::condition_variable/condition_variable_any::notify_one

是否需要同步std::condition_variable/condition_variable_any::notify_one?据我所知,如果丢失通知是可以接受的-可以调用未protectednotify_one(例如通过互斥锁)。例如,我看到了以下使用模式(抱歉,不记得在哪里):{{lock_guardl(m);//dowork}c.notify_one();}但是,我检查了libstdc++源代码,发现:condition_variable::notify_onevoidcondition_variable::notify_one()noexcept{int__e=__gthre

c++ - 使用boost程序选项时如何解决 "boost::bad_any_cast: failed conversion using boost::any_cast"?

//Usingboostprogramoptionstoreadcommandlineandconfigfiledata#includeusingnamespacestd;usingnamespaceboost;namespacepo=boost::program_options;intmain(intargc,char*argv[]){po::options_descriptionconfig("Configuration");config.add_options()("IPAddress,i","IPAddress")("Port,p","Port");po::variables_

c++ - C/C++ : any way to get reflective enums?

这种情况我遇到过很多次了……enumFruit{Apple,Banana,Pear,Tomato};现在我有Fruitf;//香蕉我想从f转到字符串"Banana";或者我有strings="Banana"并且我想从中转到Banana//枚举值或int。到目前为止,我一直在这样做。假设枚举在Fruit.h中://Fruit.cppconstchar*Fruits[]={"Apple","Banana","Pear","Tomato",NULL};显然这是一个困惑的解决方案。如果开发人员在标题中添加了一个新水果,但没有在Fruits[]中添加一个新条目(不能怪他,它们必须在两个不同的文件

c++ - boost any library 的典型用法是什么?

使用boost.any库有什么好处?你能给我一些现实生活中的例子吗?为什么不能通过在对象层次结构的根中使用一些泛型类型并使用该基类型创建容器来实现相同的功能? 最佳答案 boost::any将愉快地存储整数和float,这些类型显然没有基类。您可以使用它的真实示例是高级解释语言的虚拟机。您的“函数”对象将需要一组参数。这可以通过std::list轻松实现在幕后。 关于c++-boostanylibrary的典型用法是什么?,我们在StackOverflow上找到一个类似的问题:

c++ - is_integral vs is_integer : is one of them redundant?

is_integral和is_integer似乎以同样的方式回答同样的事情。从相关文档页面的链接来看,is_integral似乎缺少以下类型的特化signedcharunsignedcharunsignedshortunsignedintunsignedlongunsignedlonglong还有一个compiledexample,(当然)也显示了它们在这些类型上的相同行为:#include#includeusingnamespacestd;intmain(){cout::value::value::value::value::value::value如果它们的行为也相同,那么在C++

c++ - 错误 : invalid suffix "b11111111111111111111111111111111" on integer constant

我在RHEL5.7x86_64机器上使用g++版本4.1.2。这与RHEL6.0x86_64附带的g++版本4.4.5构建得很好。这个编译器错误是什么意思,如何解决?[mehoggan@hoggant35002C]$g++-Wall-obinary./binary.cpp./binary.cpp:2:5:error:invalidsuffix"b11111111111111111111111111111111"onintegerconstant./binary.cpp:3:5:error:invalidsuffix"b11111111111111111111111111111110"o

c++ - std::is_integer 和 std::is_integral 之间的区别?

C++11提供了两种类型特征模板类:std::is_integer和std::is_integral.但是,我无法分辨它们之间的区别。什么类型,比如说T,可以做成std::is_integer::value真实和使std::is_integral::value假的? 最佳答案 std::is_integer不存在。话虽如此,std::numeric_limits::is_integer确实存在。我不知道std::numeric_limits::is_integer之间有任何显着差异和std::is_integral.后者设计得晚得多