草庐IT

make_member_delegate

全部标签

c++ - std::make_unique(以及 emplace、emplace_back)对 initializer_list 参数的笨拙推导

假设我有这个结构:structposition{intx,y;};和另一个将this作为构造函数参数的类:classpositioned{public:positioned(positionp):pos(p){}private:positionpos;};我怎样才能得到简单的autobla=std::make_unique({1,2});上类?目前,编译器试图通过initializer_list匹配并调用make_unique的数组变体,这很愚蠢,因为positioned只有一个构造函数。emplace出现同样的问题和emplace_back功能。几乎所有将其可变模板参数转发给类的构造

c++ - 使用 make_shared 和 make_unique 时需要检查 nullptr 吗?

如果我使用make_shared或make_unique创建指针,我是否必须检查它是否为nullptr,例如:std::unique_ptrp=std::make_unique();if(p==nullptr){........}如果您真的内存不足,std::make_unique将通过预期。所以你永远不会从std::make_unique得到空指针。这是正确的吗?所以在执行make_shared和make_unique时不需要检查nullptr吗? 最佳答案 来自std::make_unique上的cppreference(类似于

c++ - 使用以下 has_member 函数时 SFINAE 无法正常工作的原因是什么?

我正在尝试来自WalterBrown'sTMPtalk的示例我正试图让他的has_member实现正常工作。然而,该实现似乎错误地返回true,这让我相信我不理解SFINAE的一些细节。#include#includetemplateusingvoid_t=void;templatestructhas_type_member:std::false_type{};templatestructhas_type_member>:std::true_type{};structFooWithType{typedefinttype;};structFooNoType{};intmain(){std

c++ - 实现观察者模式时出现问题 : "Member reference base type ________ is not a structure or union"

我一直在实现准系统观察者模式,但遇到了一个有点神秘的错误:“成员引用基类型‘Observer*’不是结构或union”。我认为这与我对模板的使用有关,我对模板的使用仍然相当不舒服。这是有问题的代码(为了简化事情而删除了大多数缺点/析构函数):主题界面:classSubject{public:virtualvoidnotify();private:listm_observers;};主题实现:voidSubject::notify(){list::iteratori;for(i=m_observers.begin();i!=m_observers.end();i++){*i->updat

c++ - 更改时 Make 不会重建 header

我有一个项目,我定期修改header,当我这样做时,忘记了makeclean然后make,我得到了各种奇怪的行为。我目前正在使用QtCreator作为我的IDE,但我已经在独立于Qt的项目中看到过这种情况。我的项目变得相当大,每次更改header时都必须重建,这变得效率低下。有什么想法吗?供将来引用:如果使用QMake系统:DEPENDPATH+=.\HeaderLocation1/\HeaderLocation2/\HeaderLocation2/HeaderSubLocation1/\HeaderLocation2/HeaderSubLocation2/\HeaderLocatio

c++ - 使用 MinGW 时 NetBeans Make 命令

我正在尝试使用NetBean来编写C++程序,我已经遵循了每个在这里找到的命令:https://netbeans.org/community/releases/68/cpp-setup-instructions.html#compilers_windows但是,我在使用netbeans中的make命令选项时遇到了问题。我已经将基本目录设置为M:\c++\bin,我在其中安装了MinGW,并将make命令设置为M:\MinSYS\bin\make.exe,但是在尝试构建一个简单程序时,netbeans会生成以下错误:"/M/c++/MinSYS/bin/make.exe"-fnbproj

c++ - 是否可以强制 `std::make_shared` 使用类的新运算符?

是否可以强制std::make_shared使用类的new运算符?这涉及另一个SOquestion.根据那个问题,std::make_shared使用自定义分配器:Fromthestandard(§20.7.2.2.6shared_ptrcreation):Effects:AllocatesmemorysuitableforanobjectoftypeTandconstructsanobjectinthatmemoryviatheplacementnewexpression::new(pv)T(std::forward(args)...).因此,我认为我可以使用自定义放置新运算符,但这

c++ - 带有大括号初始化的 make_unique

https://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique写道std::make_unique可以实现为templatestd::unique_ptrmake_unique(Args&&...args){returnstd::unique_ptr(newT(std::forward(args)...));}这不适用于没有构造函数的普通结构。这些可以用大括号初始化,但没有非默认构造函数。示例:#includestructpoint{intx,z;};intmain(){std::make_unique(1,2);}Com

与 Kotlin、BaseObservable 和自定义委托(delegate)的 Android 数据绑定(bind)

我正在尝试编写一个自定义委托(delegate)来清理Kotlin类中数据绑定(bind)的语法。这将消除为我可能想要观察的每个属性定义自定义getter和setter的需要。Kotlin中的标准实现如下所示:classFoo:BaseObservable(){varbar:String@Bindableget()=barset(value){bar=valuenotifyPropertyChanged(BR.bar)}}很明显,这个类有很多属性,可能会变得非常冗长。我想要的是把它抽象成这样的委托(delegate):classBaseObservableDelegate(valid:

与 Kotlin、BaseObservable 和自定义委托(delegate)的 Android 数据绑定(bind)

我正在尝试编写一个自定义委托(delegate)来清理Kotlin类中数据绑定(bind)的语法。这将消除为我可能想要观察的每个属性定义自定义getter和setter的需要。Kotlin中的标准实现如下所示:classFoo:BaseObservable(){varbar:String@Bindableget()=barset(value){bar=valuenotifyPropertyChanged(BR.bar)}}很明显,这个类有很多属性,可能会变得非常冗长。我想要的是把它抽象成这样的委托(delegate):classBaseObservableDelegate(valid: