6月1日消息,根据国外科技媒体borncity报道,根据部分网友反馈,发现在Windows系统中,由于Office、MicrosoftWord和OneDrive应用,在临时文件夹TEMP中存在容量高达数GB的 Aria-debug-xxx.log 文件。网友David在今年3月反馈,在临时文件夹下,发现了多个 Aria-debug-xxx.log(xxx随机产生)的文件,每个日志文件都有数GB。在发现并清理了这些日志文件之后,David发现微软Word应用以100MB/s的速度读取SSD文件,之后生成了一个新的日志文件。该媒体进行了调查,发现这个问题最早可以追溯到2017年,并通过排查确认了
这是一个非常简单的例子:classFoo{public:Foo(intx){};};voidProcessFoo(Foo&foo){}intmain(){ProcessFoo(Foo(42));return0;}以上在VisualStudio上编译正常,但在Linux和Mac上会产生错误。编译上面的代码会生成这个:$g++-std=c++11-cnewfile.cppnewfile.cpp:Infunction‘intmain()’:newfile.cpp:23:23:error:invalidinitializationofnon-constreferenceoftype‘Foo&’
这是一个非常简单的例子:classFoo{public:Foo(intx){};};voidProcessFoo(Foo&foo){}intmain(){ProcessFoo(Foo(42));return0;}以上在VisualStudio上编译正常,但在Linux和Mac上会产生错误。编译上面的代码会生成这个:$g++-std=c++11-cnewfile.cppnewfile.cpp:Infunction‘intmain()’:newfile.cpp:23:23:error:invalidinitializationofnon-constreferenceoftype‘Foo&’
有问题的代码是这样的:structsomething_bad_happened_exception:std::exception{};voidfoo(){something_bad_happened_exceptione;throwe;}clang发出警告,内容如下:Throwexpressionshouldthrowanonymoustemporaryvaluesinstead[cert-err09-cpp]这意味着foo()应该改为:voidfoo(){throwsomething_bad_happened_exception();}为什么抛出一个临时变量而不是一个局部变量更好?
有问题的代码是这样的:structsomething_bad_happened_exception:std::exception{};voidfoo(){something_bad_happened_exceptione;throwe;}clang发出警告,内容如下:Throwexpressionshouldthrowanonymoustemporaryvaluesinstead[cert-err09-cpp]这意味着foo()应该改为:voidfoo(){throwsomething_bad_happened_exception();}为什么抛出一个临时变量而不是一个局部变量更好?
考虑一个可以用作范围的简单类A:structA{~A(){std::cout如果我在range-for中创建一个临时A,它的工作原理与我希望的完全一样:for(autoc:A{"works"}){std::cout但是,如果我尝试包装临时:structwrap{wrap(A&&a):a(std::move(a)){}constchar*begin()const{returna.begin();}constchar*end()const{returna.end();}A&&a;};for(autoc:wrap(A{"fails"})){std::cout为什么A的生命周期没有针对整个范围
考虑一个可以用作范围的简单类A:structA{~A(){std::cout如果我在range-for中创建一个临时A,它的工作原理与我希望的完全一样:for(autoc:A{"works"}){std::cout但是,如果我尝试包装临时:structwrap{wrap(A&&a):a(std::move(a)){}constchar*begin()const{returna.begin();}constchar*end()const{returna.end();}A&&a;};for(autoc:wrap(A{"fails"})){std::cout为什么A的生命周期没有针对整个范围
在VisualC++2017(使用/std:c++14或使用/std:c++17)中,以下代码有效:voidTakePtr(char*);//constornotintmain(){TakePtr(char{});TakePtr(char());}我不明白为什么会这样。显然,以下方法也可以(如预期的那样):voidTakeChar(char);TakeChar(char{});TakeChar(char());当char{}或char()用作参数?现在,如果我同时有char和char*重载,它可以正常工作而不会出现任何关于歧义的错误/警告:voidTakePtr(char*);void
在VisualC++2017(使用/std:c++14或使用/std:c++17)中,以下代码有效:voidTakePtr(char*);//constornotintmain(){TakePtr(char{});TakePtr(char());}我不明白为什么会这样。显然,以下方法也可以(如预期的那样):voidTakeChar(char);TakeChar(char{});TakeChar(char());当char{}或char()用作参数?现在,如果我同时有char和char*重载,它可以正常工作而不会出现任何关于歧义的错误/警告:voidTakePtr(char*);void
这个问题在这里已经有了答案:Aboutbindingaconstreferencetoasub-objectofatemporary(3个回答)关闭6年前。常量引用延长函数返回的临时对象的生命周期是C++的knownfeature,但是对函数返回的临时对象的成员使用常量引用是否可以接受?示例:#includestd::pairgetPair(intn){return{std::to_string(n),n};}intmain(int,char*[]){constintx=123456;constauto&str=getPair(x).first;printf("%d=%s\n",x,s