草庐IT

c++ - 为什么 std::shared_ptr 的别名构造函数不初始化 std::enabled_shared_from_this?

考虑以下代码:structFoo:std::enable_shared_from_this{};structBar{Foofoo;};intmain(){std::shared_ptrbar_p(newBar);//makeshared_ptrtomemberwithaliasingconstructorstd::shared_ptrfoo_p(bar_p,&bar_p->foo);assert(bar_p->foo.shared_from_this());//fail!throwsbad_weak_ptr}不幸的是,它没有按预期工作(至少在GCC4.8.2中)。我查看了代码,似乎别名

c++ - enable_if 检查迭代器的值类型是否是一对

我想为值类型为一对的迭代器编写一个专门的模板函数。我的期望是这应该匹配std::map的迭代器。为了检测对:templatestructis_pair:std::false_type{};templatestructis_pair>:std::true_type{};//alsotriedthis,butitdidn'thelptemplatestructis_pair>:std::true_type{};然后我在函数声明中使用enable_if:templatedecltype(auto)do_stuff(std::enable_if::value,ITR>itr){//access

c++ - 使用 enable_if 的模板特化在 Clang 中失败,适用于 GCC

我正在尝试删除基于模板类型的成员函数。问题是在未删除的情况下,使以后的模板特化与我的函数的类型签名匹配。我尝试了以下代码,它使用GCC(9.0.1)编译,但在Clang(9.0.0)中出错。我认为它也无法在MSVC++中构建代码。#include#includetemplatestructmy_type{templatestd::enable_if_t::value,my_type>my_fun(constmy_type&v){std::couttemplatestd::enable_if_t::value,my_type>my_type::my_fun(constmy_type&v)

c++ - `enable_if` 与 `enum` 模板特化问题

我在GCC编译时遇到问题enable_ifs应用于模板类方法的返回值。使用Clang,我可以在enable_if中使用表达式在enum上模板参数,而GCC拒绝编译此代码。这里是问题描述、初始代码及其后续修改,试图让我和编译器满意(不幸的是,不是同时)。我有一个非模板类Logic包含模板化类方法computeThings()它有一个enumStrategy作为其模板参数的之一。computeThings()中的逻辑取决于编译时间Strategy,所以ifconstexpr是一种合理的实现方式。变体1#includeclassLogic{public:enumStrategy{strat_

c++ - enable_if 的句法模式

我一直以这种近似的方式对各种版本的GCC(最高5.2)使用enable_if:template...>voidfn(){std::cout...>voidfn(){std::cout();fn();但是,事实证明,Clang3.7不接受这一点(“对‘fn’的调用不明确”)。Q1。谁是对的,为什么?当然还有其他方法可以做到,但我有点不喜欢templatestd::enable_if_tfa(){std::cout及其类似的方法使函数签名的正常部分更难阅读,并且template=0>voidfd(){std::cout用于涉及不相关的元素(类型和值)。Q2。还有哪些其他(正确、更易读、更少

C++-14 使用 enable_if_t 选择整数类型模板化类的成员函数

我正在尝试基于一个完整的类模板参数启用不同的成员函数,如下所示:#includetemplatestructFoo{template=0>intbar(inti)const{returni;}template=0>intbar(inti,intj)const{returni+j;}};intmain(intargc,constchar**argv){Fooa;a.bar(1);Foob;b.bar(1,2);return0;}在c++-14模式下使用gcc5,编译失败,出现如下错误:tools/t1.cpp:Ininstantiationof'structFoo':tools/t1.c

c++ - 为什么这个涉及 std::enable_if 的模板元函数会产生不希望的结果?

我有一个类型特征templatestructis_binary_function:std::false_type{};及其专长templatestructis_binary_function&&!std::is_void_v&&!std::is_void_v&&function_t::isBinaryCallable,function_t>>:std::true_type{};我正在尝试识别具有公共(public)类型定义result_t、parameter1_t和parameter2_t以及静态常量的类isBinaryCallable值为true。然而,下面的代码没有输出我所期望的:

c++ - 将从唯一指针移动到共享指针也会初始化 enable_shared_from_this

当我继承std::enable_shared_from_this,但是我创建了一个unique_ptr,std::enable_shared_from_this里面的weak_ptr也会被初始化吗当我通过std::move或移动构造函数“移动”到shared_ptr时?例如下面的代码会发生什么:#include#includeclassA:publicstd::enable_shared_from_this{public:std::shared_ptrgetA(){returnshared_from_this();}};intmain(){std::unique_ptru(newA()

c++ - 使用 std::enable_if 和可变参数基类

这是我正在尝试做的一个简化示例:#include#include#includetemplateclassfoo{public:templatetypenamestd::enable_if::value>::typebar(constU&t){std::coutclassbaz:publicfoo...{};intmain(){bazb;b.bar(1.0);}这给了我模棱两可的函数错误:error:requestformember'bar'isambiguousb.bar(1.0);note:candidatesare:templatetypenamestd::enable_if::

c++ - 使用 std::enable_if 有什么问题?

我有一个函数set_data,我必须针对它接收的不同类型以不同的方式实现它。例如,它试图根据输入类型实现两个重载。如果是基本的,非void和非nullptr_t,则在第一次实现时处理它。如果它是std::string或char缓冲区,则以第二种方式处理它。structfield{templatevoidset_data(TDataTypedata,size_tindex=0);};template::value&&std::is_same::value==false&&std::is_same::value==false>::type>voidfield::set_data(TData