草庐IT

Auto-generated

全部标签

c++ - 使用 C++11 auto 作为 const 函数对象的返回类型

我有一个const函数对象,暂时它返回void。但可以返回int或double。我正在用c++11风格编写代码,只是想使用auto作为返回类型。尽管代码可以编译,但我不确定它是否100%正确。这是代码。templatestructmy_func{public:my_func(){}my_func(graph_t&_G):G(_G){}templateautooperator()(edge_tedge)->voidconst{//dosomethingwiththeedge.}//operatorprivate:graph_t&G;};//callthefunctor:(passgrap

c++ - 为什么 unique_ptr 对 auto_ptr 有重载?

我遇到编译器错误并注意到一些有趣的事情。出于某种原因,unique_ptr对auto_ptr有重载,但我认为auto_ptr已被弃用:/usr/local/include/c++/4.9.0/bits/unique_ptr.h:228:2:note:templatestd::unique_ptr::unique_ptr(std::auto_ptr&&)unique_ptr(auto_ptr&&__u)noexcept;/usr/local/include/c++/4.9.0/bits/unique_ptr.h:228:2:note:templateargumentdeduction/s

c++ - inline static auto 的 Initializer "sizeof(T)"...是否需要实例化?

如果表达式的类型不相关,但我们用它来初始化静态自动变量,会发生什么?GCC和Clang的行为不同templatestructA{staticinlineautox=sizeof(T{}.f);};Aa;GCC不会引发错误。但是Clang认为这是无效的,因为它实例化了“sizeof”的操作数。GCC似乎跳过了该步骤,因为sizeof(T{}.f)始终具有类型size_t(不依赖于类型),因此它已经知道x没有实例化。如果我们引用x,例如(void)a.x;,两个编译器都会拒绝该程序。它甚至必须解析x的类型吗?如果我没记错的话,C++14以上的语言允许使用“占位符类型”保留事物(如函数)并进

c++ - `template <auto>` 和部分类模板特化排序

考虑:#includetemplatestructTag{};templateautotag=Tag{};templatestructSelectorImpl;//1templatestructSelectorImpl...>{};//2template*tag,auto...xs>structSelectorImpl,std::integral_constant...>{};templatestructSelector:SelectorImpl...>{};intmain(){Selector,1,2>{};}gcc和clang都无法编译它,报告SelectorImpl的特化不明确。

c++ - decltype(auto) 函数返回类型不推导 && 类型

如果一个函数返回decltype(auto),它返回一个int&&类型的局部变量,为什么返回类型是int&?如果我将变量转换为它自己的类型,那么返回类型就是我所期望的(int&&)#includenamespace{autoi=5;autoj=5;decltype(auto)foo1(){int&&ret=std::move(i);returnret;}decltype(auto)foo2(){int&&ret=std::move(j);returnstatic_cast(ret);}}intmain(){static_assert(std::is_same_v);static_ass

c++ - boost::uuids::random_generator 线程安全吗?

考虑使用g++-std=c++11(GCC4.7.2)编译的函数:boost::uuids::uuidgetID(){staticboost::uuids::random_generatorgenerator;returngenerator();}从多个线程调用getID是否安全?如前所述here根据C++11标准,第一行的局部静态对象定义是线程安全的。问题是在第二行对同一对象generator调用boost::uuids::random_generator::operator()是否也是线程安全的。返回的UUID在单个线程中是否是唯一的? 最佳答案

c++ - 未定义对 'uuid_generate' 的引用,即使我使用 -luuid

这个问题在这里已经有了答案:Whatisanundefinedreference/unresolvedexternalsymbolerrorandhowdoIfixit?(38个答案)关闭7年前。我的测试.cpp#include#includeintmain(intargc,char*argv[]){uuid_tid;uuid_generate(id);char*string=newchar[100];uuid_unparse(id,string);std::cout我正在使用Ubuntu14我正在运行我的test.cpp作为...g++-luuidtest.cpp和输出test.cp

c++ - 大 vector 的返回值优化与 auto_ptr

如果我使用auto_ptr作为填充大vector的函数的返回值,这会使该函数成为源函数(它将创建一个内部auto_ptr并在返回非constauto_ptr时移交所有权)。但是,我不能将此函数与STL算法一起使用,因为为了访问数据,我需要取消对auto_ptr的引用。我猜一个很好的例子是大小为N的vector场,每个vector有100个分量。如果N很大,则函数按值或按ref返回每个100个分量vector是不一样的。此外,当我尝试这个非常基本的代码时:classt{public:t(){std::coutautoFun(){returnstd::auto_ptr(newt());}a

C++11: `auto` 对 const 和引用类型的操作的标准引用

假设我有一个类型T:typedef...T;然后我有这些功能:Tf11();T&f12();T&&f13();constTf21();constT&f22();constT&&f23();然后像这样称呼他们:autox11=f11();autox12=f12();autox13=f13();autox21=f21();autox22=f22();autox23=f23();从C++11标准的哪些部分/条款可以推导出x11..x23的等效非自动声明? 最佳答案 它在§7.1.6.4autospecifier中。在您的函数返回类型示例中

c++ - 有没有办法为 C++03 实现 auto 关键字

在C++03或更早版本中,是否有实现auto关键字的方法?不是对象类,而是可以像这样使用它[C++11]autox=5;std::cout我很快“搞定”了一个实现,但它很垃圾,因为你可以将它转换为任何类型-太像一个object类,而且非常基础,我知道,但无论如何,这里是:classauto_t{public:templateauto_t(const_Ty&_Value):__data(_Value){}templateoperator_Ty(){return(_Ty)__data;}private:void*__data;};#defineautoauto_t