enable-stdio-inheritance
全部标签 简介:C++标准区分依赖模板参数的符号名称和不依赖模板参数的名称,这称为两阶段名称查找(参见here)。定义模板时,会尽快解析非相关名称。另一方面,从属名称仅在模板实例化时解析。示例:templatestructBase{typedefTtype;staticconstintn=3;virtualintf()=0;intf(intx){returnx*2;}};//doesn'tcompile!templatestructDerived:Base{typefield;//Thecompilerdoesn'tknowBase::typeyet!intf(){returnn;}//thec
我一直以这种近似的方式对各种版本的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。还有哪些其他(正确、更易读、更少
我想编写一个可以操作不同类型数据库(例如sqlite、postgres等)的数据库包装器,因此无论用户实际使用什么数据库,他们编写的代码都不会改变。在我看来,这需要一个抽象基类,例如:classdatabase{public:virtualboolquery(conststd::string&q)=0;//Otherstuff};classsqlite:publicdatabase{public:boolquery(conststd::string&q){//Implementation}};这看起来不错,但我正在使用可变参数模板来转义查询中的参数(我真的很喜欢这个想法,所以我想坚持下
我正在尝试基于一个完整的类模板参数启用不同的成员函数,如下所示:#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
我有一个类型特征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。然而,下面的代码没有输出我所期望的:
当我继承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()
在编写框架时遇到以下问题:我有classA和classB派生自classA。classA有一个返回B*的函数。当然,这并不难:#includeusingnamespacestd;classB;//forwarddeclarationclassA{public:B*ReturnSomeData();};classB:publicA{};//Implementation:B*A::ReturnSomeData(){returnnewB;//doesn'tmatterhowthefunctionmakespointer}intmain(){Asth;cout但是我不得不使用像这里这样的模板:
这是我正在尝试做的一个简化示例:#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::
我有一个函数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
我编写了一个类来促进具有以下构造函数的类型删除:classEnvelope{public:Envelope(){}templateEnvelope(Runnablerunnable):m_runFunc(&Envelope::RunAndDeleteRunnable),m_runnable(newRunnable(runnable)){}templateEnvelope(Runnable*runnable):m_runFunc(&Envelope::RunRunnable),m_runnable(runnable){}};我想重写第一个非默认构造函数以获取引用而不是值(Runnable