草庐IT

c++ - 模板隐式转换

代码:templateclassMyClass{public:MyClass(constMyClass&other){//Explicit,typesmustbothmatch;}templateMyClass(constMyClass&other){//Implicit,typescancopyacrossifassignable.//Isthereawaytomakethisautomaticallyhappenformemberwise//copyingwithouthavingtodefinethecontentsofthisfunction?this->value=other

c++ - 在 C++ 中禁用隐式 this

我收到了一个遗留的C++应用程序来修补和添加一些新功能,我在遵循一些代码时遇到了一段糟糕的时光,因为它相当广泛地使用了全局变量,巨大的#define宏和许多极其简洁命名的变量/函数(来自2个继承级别的3个字母函数,等等...)。因此,确定许多函数或变量的来源具有挑战性。它还使用匈牙利符号....有时(m_Thingie是一个成员变量,但有时也是thingie)。有什么方法可以使不指定this->的类成员访问失败吗?那会让我使用编译器来有效地确定变量源。我不介意它是否是一个可怕的hack,如果我可以在进行重构时打开它一会儿,然后在任何发布编译时关闭它,那会很好。

c++ - 为什么隐式声明不引入名称?

之前看了下面两个问题,还是没看懂。1.WhydoIneedto#includewhenusingthetypeidoperator?2.Whenis#includelibraryrequiredinC++?自typeid使用type_info类,要求我们是合理的#include.但是,newoperators还使用std::bad_alloc,为什么它不需要我们#include?(我知道sizeof()不需要因为size_t可以在编译期间用内置类型替换,只要编译器知道size_t实际上是什么。)根据第二个问题中投票最多的答案,他说:Note:theimplicitdeclaration

c++ - 隐式转换以适应现有构造函数

关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭4年前。Improvethisquestion我的主要目标是拥有类似的东西:std::chrono::high_resolution_clocktp4{2013,1,2,10,11,12,-1,1234};所以我创建了一个包含tm对象的类,如下所示:structTm:std::tm{inttm_usecs;//[0,999999]microsafterthesecTm(constintyear,constintmonth,constintmd

c++ - 隐式转换产生 "error: taking address of temporary"(GCC vs clang)

在试验强类型整数时,我遇到了一个来自GCC8.2的奇怪错误:error:takingaddressoftemporary我可以想象上述错误有意义的典型场景,但在我的情况下我没有遇到问题。重现错误的缩小(人为)示例如下:#include#includeenumclassEnum:std::size_t{};structPod{std::size_tval;constexproperatorEnum()const{returnstatic_cast(val);}};templateconstexprvoidfoo(){usingFoo=std::integral_constant;//[G

c++ - C++中是否存在任何隐式内存障碍

在下面的代码中,是使用必要的原子来保证所有平台上的无竞争语义,还是使用promise.set_value/future.wait暗示某种隐式内存屏障,这将允许我依赖标志写入对外线程可见?std::atomic_boolflag{false};//voidrunInThreadPoolBlocking(Callablefunc){std::promiseprom;autofut=prom.get_future();enqueueToThreadPool([&](){func();prom.set_value();});fut.get();}一般来说,对于thread.join()或fut

c++ - 模板化时没有发生隐式转换

这里有几个相关但不同的类。一个包含float列表;一个只包含一个。有时我想,比如说,将它们相乘。在那种情况下,我想将非列表“提升”到列表中。这是代码,它按照我想要的方式工作。#defineLIST_SZ4classVec1;classVec1_list{public:Vec1_list(){}Vec1_list(constVec1&in);floatx[LIST_SZ];};classVec1{public:Vec1(){}Vec1(constfloat&in);floatx;};Vec1::Vec1(constfloat&in){x=in;}Vec1_list::Vec1_list(

c++ - 在没有 RValue 隐式转换的情况下正确实现

我遇到了RValue不允许隐式转换的问题。我的问题是什么实现更好地“绕过”这个限制?下面是说明问题的示例代码:templateclassITestClass{public:virtualvoidmyFunc(myValitem)=0;virtualmyValmyFunc1()=0;};classCTestClass:publicITestClass{public:voidmyFunc(intitem){}intmyFunc1(){return0;}};templateinlineintCallFunction(std::shared_ptr>ptrBase){return0;}inli

参数列表中的 C++ 隐式类型转换

我对C++参数列表的隐式类型转换如何工作感到困惑。特别是,我有一堆函数称为inRange(x,start,end),它们根据x是否在开始和结束之间返回一个bool值。[在此描述中,inRange只是(x>start&&x我对上面的类型含糊不清。特别是整数和浮点比较有不同的实现,这意味着模板并不是真正合适的,因为没有C++语言分组来区分int/long/unsigned/size_t等与float/double等。所以我尝试了通过定义具有足够宽的int/float类型的两个版本的inRange来使用类型系统:inlineboolinRange(longx,longstart,longen

c++ - 如何对成员函数使用隐式类型转换?

让我们考虑以下示例,了解隐式类型转换有效和无效的情况:#include#includestructThingy{voidwrite(){std::coutvoidf(TIteratorbegin,TIteratorend){for(TIteratorit=begin;it!=end;++it)it->write();}intmain(){std::vectorvector(10);f(vector.begin(),vector.end());//Doesn'tcompilef(vector[3]);//compilesvector[3].write();//Doesn'tcompile