草庐IT

any_variable

全部标签

C++11 可变参数模板 : return tuple from variable list of vectors

我想写一些类似于pythonzip(http://docs.python.org/2/library/functions.html)的东西。zip应该接受可变数量的不同类型的vector,并返回一个vector元组,截断到最短输入的长度。例如x=[1,2,3]v=['a','b']我希望输出是一个vector[,]如何在C++11中做到这一点? 最佳答案 急切地做到这一点并且只通过复制非常容易:#include#include#includetemplatestd::vector>zip(std::vectorconst&...vs

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++ - scoped_lock 如何避免发出 "unused variable"警告?

boost::mutex::scoped_lock是一个方便的RAII包装器,用于锁定互斥锁。我对其他事情使用了类似的技术:一个RAII包装器,它要求数据接口(interface)从/重新连接到串行设备。不过,我想不通的是,为什么在下面的代码中只有我的对象mst(其实例化和销毁确实有副作用)会导致g++发出“未使用的变量”警告错误,而l设法保持沉默。你知道吗?你能告诉我吗?[generic@sentinel~]$cattest.cpp#include#include#includestructMyScopedThing;structMyWorkerObject{voida(){std:

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++ 标准 : do namespace-scoped constexpr variables have internal linkage?

假设我们有一个标题foo.h包含以下内容:#ifndefFOO_H_#defineFOO_H_namespacefoo{constexprstd::string_viewkSomeString="blah";}#endif//FOO_H_foo::kSomeString是否保证在包含foo.h的任何翻译单元中具有内部链接?这在C++11和C++17之间是否有所不同?在标准草案中[basic.link]/3说Anamehavingnamespacescopehasinternallinkageifitisthenameof[...]anon-inlinevariableofnon-vol

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++ - 这个 "if e is a pack, then get a template name, otherwise get a variable name"是否有效?

我尝试构建一个不需要typename或template的案例,但仍会根据给定名称t生成变量或模板是否为函数参数包templatestructA{templatestaticvoidf(int){}};templatestructA{staticconstintf=0;};templateusingtype=int;templatevoidf(Tt){A...)>::f(1);}intmain(){f(1);}以上将引用staticconstint,并进行比较。以下刚好有Tt变成了一个包并制作f引用模板,但GCC也不喜欢templatevoidf(T...t){A...)>::f(1);

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++ - 警告 C4996 : This function or variable may be unsafe -- compared to GCC on POSIX

我注意到MS编译器会为cstdlib函数(如getenv)发出“已弃用”警告。MS发明了自己的标准,例如_dupenv_s。问题1AFAIK主要的“不安全”事情是关于重入*。既然MS的CRT被标记为“多线程”(/MT),他们为什么不直接将getenv替换为可重入的线程安全版本呢?是否有人会依赖不安全的行为?问题2我用GCCg++-Wall-Wextra-Weff++-pedanticfoo.cpp编译了相同的代码,它不会产生任何警告。所以我想这在POSIX上不是问题吗?这是如何解决的?(好吧,也许他们只是改变了getenv的行为,很高兴能得到确认。*说它只是关于可重入性是一种过度概括。

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

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