草庐IT

Duck-typing

全部标签

c++ - 为什么 C++1* 仍然需要 template 关键字来代替 Full Duck Typing

很多年前,(至少对我而言)静态C++多态性似乎是连贯的。Python等语言依赖ducktyping,你有:deffn(foo,bar):foo.baz(bar.bo())当时的想法是,如果它适本地“嘎嘎叫”,那么语言就没问题。相反,在C++中,您必须解释它是什么“动物”:voidfn(foo_typefoo,bar_typebar);对于“家庭王国”,您明确需要使用template关键字:templatevoidfn(Foofoo,Barbar);具有像auto...()->decltype这样的新功能返回类型,尤其是genericlambdas,似乎有一些更像是非模板Python类的

c++ - 定义两个变量 : Is "Type a(arg), b(arg);" completely equivalent to "Type a(arg); Type b(arg);"?

假设我想定义两个{Type}类的变量。构造函数采用1个参数。下面两种方式是否完全等价(编译成相同的目标代码)?Typea(arg),b(arg);和Typea(arg);Typeb(arg);这个问题是在我阅读了一个讨论异常安全的页面后出现的---http://www.gotw.ca/gotw/056.htm有一个指南“在其自己的代码语句中执行每个资源分配(例如,新的),立即将新资源提供给管理器对象。”它举了一个例子:以下代码段是安全的auto_ptrt1(newT);auto_ptrt2(newT);f(t1,t2);但是下面这行是不安全的f(auto_ptr(newT),auto_

c++ - "const T& operator[](size_type i)"中的 const 有什么用?

我在一本书http://www.acceleratedcpp.com/中发现了这个有趣的行-资源-第11章-Vec.h(我是一个std::vector翻版)而且我真的不明白这个版本的运算符有什么用。为什么要定义此运算符的两个版本(常量和非常量)?我什至试过了,在我看来,非常量版本一直被调用......你能解释一下吗?#include#include#include#includeusingnamespacestd;templateclassVec{public:typedefT*iterator;typedefconstT*const_iterator;typedefsize_tsiz

c++ - 错误 C2678 : binary '==' : no operator found which takes a left-hand operand of type (or there is no acceptable conversion)

我正在尝试编译以下代码:#include#include#includetypedefboost::geometry::model::d2::point_xyPoint;typedefstd::pairVector;booloperator==(constPoint&p1,constPoint&p2){returnp1.x()==p2.x()&&p1.y()==p2.y();}intmain(){Vectorvec1(Point(0,0),Point(1,1));Vectorvec2(Point(0,0),Point(1,2));std::coutVS2012C++编译器返回以下编译错

c++ - 海湾合作委员会 : Unscoped enumeration type give an ambiguity error

在下面的代码中,我定义了一个unscopedenumeration为longlong类型。该程序在Clang上运行良好。但是GCC编译器给出了一个歧义错误。#includeenum:longlong{Var=5};voidfun(longlongll){std::coutGCC产生的错误:main.cpp:Infunction'intmain()':main.cpp:17:12:error:callofoverloaded'fun()'isambiguousfun(Var);^main.cpp:5:6:note:candidate:voidfun(longlongint)voidfun

c++ - 自动返回类型扣除警告 : why do we need decltype when return defines the type anyway?

这是一个关于elementsSize()成员函数做什么的问题,关于自动返回类型推导:#include#includetemplateclassElementVector{std::vectorelementVec_;//Otherattributes.public:ElementVector()=default;ElementVector(conststd::initializer_list&list):elementVec_(list){}autoelementsSize()//->decltype(elementVec_size()){returnelementVec_.size(

C++ 错误 : Incompatible types in assignment of ‘char*’ to ‘char [2]

我的构造函数有点问题。在我的头文件中,我声明:charshort_name_[2];和其他变量在我的构造函数中:Territory(std::stringname,charshort_name[2],Player*owner,charunits);voidsetShortName(char*short_name);inlineconstchar(&getShortName()const)[2]{returnshort_name_;}在我的cpp文件中:Territory::Territory(std::stringname,charshort_name[2],Player*owner,

c++ - Type** name 和 Type* name[] 有什么区别?

Type**name和Type*name[]有什么区别?为什么有人会使用一个而不是另一个?谢谢 最佳答案 这取决于它是在变量声明中还是在函数参数中?如果在变量声明中:Type**name=&pointer_to_type;Type*name[]={&pointer_to_type,0,&pointer_to_type};第一个是指向类型的指针,而第二个是指向长度为3的类型的指针数组。如果在函数参数中,它们是一样的。数组衰减为指针,Type**name和Type*name[]与函数参数完全相同。但是,第二种形式清楚地表明name是一个

c++ - 错误 : c++ [map] does not name a type

我编写了以下内容作为文本冒险游戏的文本命令解析器的一部分。我试图将用户输入的字符串与枚举类中的项目相关联。以下是我的头文件:#include#include#includeusingnamespacestd;enumclassNoun{//Interrogationsubjectsname,//askthesubjecthisnamebase,//whereisthebase?attack,//whenwilltheattackbe?invalid};mapknownNouns;knownNouns["name"]=Noun::name;knownNouns["base"]=Noun:

c++ - 函数/方法重载 C++ : Data type confusion?

我在C++中重载方法时遇到了一些问题。作为问题的一个例子,我有一个类,其中有许多方法被重载,并且每个方法都有一个具有不同数据类型的参数。我的问题:这些方法应该出现在类中是否有特定的顺序,以确保根据其参数数据类型调用正确的方法?classSomeClass{public:...voidMethod(boolparamater);voidMethod(std::stringparamater);voidMethod(uint64_tparamater);voidMethod(int64_tparamater);voidMethod(uint8_tparamater);voidMethod(