草庐IT

move_if_noexcept

全部标签

c++ - 如何正确使用enable_if?

我需要学习如何使用enable_if。为此,我需要使用enable_if重新实现距离函数。我试过这个:#include#include#include#include#includetemplatetypenamestd::enable_if::value,std::iterator_traits::difference_type>::typemy_distance(Inbegin,Inend,std::input_iterator_tagdummy){typenamestd::iterator_traits::difference_typen=0;while(begin!=end){

c++ - std::move 内部 move 赋值运算符

我读入了anotherquestion在实现move构造函数时,最好对初始化列表中的每个成员进行std::move,因为如果该成员恰好是另一个对象,那么将调用该对象的move构造函数。像这样...//MoveconstructorCar::Car(Car&&obj):prBufferLength(std::move(obj.prBufferLength)),prBuffer(std::move(obj.prBuffer)){obj.prBuffer=nullptr;obj.prBufferLength=0;}然而,在我见过的所有示例move赋值运算符中,都没有提到使用std::move

C++11 Dynamic Cast If Else Chain -> 开关

考虑以下几点:structB{};templatestructD:B{Tt;}voidg(inti){...}voidg(strings){...}voidg(charc){...}voidf(B*b){if(dynamic_cast*>(b)){g(dynamic_cast*>(b)->t);}elseif(dynamic_cast*>(b)){g(dynamic_cast*>(b)->t);}elseif(dynamic_cast*>(b)){g(dynamic_cast*>(c)->t)}elsethrowerror;};这里只有三种可能的T类型——int、string、char

c++ - 无限递归 `enable_if`

在尝试为另一种类型T编写wrapper类型时,我遇到了一个相当令人讨厌的问题:我想定义一些二元运算符(例如+)将wrapper上的任何操作转发给底层类型,但我需要这些运算符接受任何涉及wrapper的潜在组合:wrapper()+wrapper()wrapper()+T()T()+wrapper()天真的方法涉及直接编写所有潜在的重载。但我不喜欢编写重复的代码并且想要更多的挑战,所以我选择使用一个非常通用的模板来实现它并使用enable_if限制潜在的类型。我的尝试显示在问题的底部(抱歉,这是我能想到的最小值)。问题是它会遇到无限递归错误:为了评估test()+test(),编译会查看

c++ - 为什么 std 智能指针类型析构函数不继承指向对象的 noexcept dtor 状态

在C++11中,我的理解是默认情况下析构函数是隐式的noexcept(true),除了:如果我有一个类C有一个显式标记为noexcept(false)的析构函数(大概是因为它出于某种奇怪的原因抛出,我知道你不应该,为什么)然后是派生自C的任何类的析构函数或包含C类型的成员也变成noexcept(false).但是,一个包含std::shared_ptr的类显然不会自动将其析构函数切换为noexcept(false),包含std::weak_ptr也是如此,std::unique_ptr等这是一个完整的例子:#include#includestructNormal{~Normal(){}

c++ - 为什么 vector 被认为是 nothrow_move_constructible?

vector的移动构造函数的规范是(从标准中复制出来的):vector(vector&&);注意缺少noexcept.但是gcc4.8和Clang3.2都报告了std::is_nothrow_move_constructible>::value返回真(即1):#include#includeintmain(){std::cout>::value造成这种明显差异的原因是什么? 最佳答案 标准允许实现根据方法加强异常规范17.6.5.12Restrictionsonexceptionhandling[res.on.exception.h

c++ - 编译器是否可以在仅引用一次时隐式 std::move 参数?

假设我有一个带有setter的简单类:classMyClass{public:voidsetName(std::stringname){_name=std::move(name);}private:std::string_name;};我在这里使用std::move,但是如果我忽略它而只写_name=name,编译器是否可以隐式movename参数,因为它没有在setter的其他任何地方使用?它几乎可以被视为赋值表达式中的右值,因为它在其他任何地方都没有被名称引用。编译器能做到吗?现有的编译器会这样做吗? 最佳答案 在什么之上添加N

c++ - 如何在类 noexcept 之外定义默认构造函数?

我知道标记为=default的构造函数将尽可能“尝试”为noexcept。但是,如果我在类的外部定义它,它就不再是noexcept,您可以从这段代码中看到:#include#include#includestructBar{Bar()=default;Bar(Bar&&)=default;//noexcept};structFoo{Foo()=default;Foo(Foo&&);};//movingthedefinitionoutsidemakesitnoexcept(false)Foo::Foo(Foo&&)=default;//notnoexceptanymoreintmain(

c++ - 是否可以从 std::string 中获取内存(就像 string move ctor 那样)?

如果我的内部类是我自己的vector版本(我控制来源)并且为了举例,我不能将其更改为std::string有没有办法从std::string窃取内存,就像std::string的move构造函数一样做。所以像这样:std::stringstr{"abcdefghijklmnopqrstu"};MyVectorCharClassmvc(std::move(str));//Constructortakesmemoryfromstr我想我听说过一些future的建议,以添加.release()至std::string或std::vector但我说的是现在。 最佳答

c++ - 从类中返回可 move 的成员变量

classfoo{public:barsteal_the_moveable_object();private:barmoveable_object;};main(){foof;automoved_object=f.steal_the_moveable_object();}如何实现steal_the_movebale_object将moveable_objectmove到moved_object中? 最佳答案 您可以直接在返回语句中简单地move成员:classfoo{public:barsteal_the_moveable_obje