草庐IT

Duck-typing

全部标签

c++ - 为什么 type_traits 是用专门的模板结构而不是 constexpr 实现的?

标准将它们指定为模板struct而不是简单的bool值constexpr有什么原因吗?在一个可能会在主要问题的良好答案中得到回答的附加问题中,如何对非结构版本执行enable_if操作? 最佳答案 一个原因是constexpr函数无法提供嵌套的type成员,这在某些元编程情况下很有用。为了清楚起见,我不是只谈论产生类型但显然不能产生类型的转换特征(如make_unsigned)constexpr功能。所有类型特征都提供了这样一个嵌套的type成员,甚至是一元类型特征和二进制类型特征。例如is_void::type是false_typ

c++ - 我可以告诉在 C++ 中使用 "new type[n]"创建的数组的大小吗?

使用C++11,我喜欢创建一个bool数组并立即清除它bool*mymap=newbool[n];n是可变的。现在,这是否已经清除了数组占用的内存?如果不是,是否有比对所有元素使用循环并将每个元素分别设置为false更好的方法来清除数组?我考虑过使用std:memset(),但这需要我知道数组的大小。现在,初学者可能会说:简单,大小是n*sizeof(bool)。但我不买那个。编译器可能决定以不同方式打包它们,甚至将它们打包为位,不是吗?那么,有没有办法更清楚地告诉数组的大小呢?我想可能有一个std:arraysize()函数,它只返回内存中分配的数组的空间。毕竟,必须以某种方式在运行

c++ - g++ "is not a type"错误

编写一个模板函数,我声明:templateTinvertible(Tconst&container,T::size_typestartIndex,T::size_typeendIndex);使用g++4.0.1编译时出现错误:error:'T::size_type'isnotatype 最佳答案 您需要添加类型名称。即templateTinvertible(Tconst&container,typenameT::size_typestartIndex,typenameT::size_typeendIndex);在没有关于类型T的任何

c++ - 铿锵错误 : non-type template argument refers to function that does not have linkage -- bug?

我有一些非常简单的(C++11)代码,最新的clang(version3.4trunk187493)无法编译,但GCC编译正常。代码(下面)实例化函数模板foo使用局部函数类型Bar然后尝试将其地址用作类模板Func的非类型模板参数:templatestructFunc{};templateexterninlinevoidfoo(){usingFoo=Func>;}intmain(){structBar{};//function-localtypefoo();return0;}clang发出以下错误:error:non-typetemplateargumentreferstofunct

c++ - 重载运算符 : const vs non-const return type : any difference of performance?

如果我们去维基百科article关于C++运算符,我们有一个例子:Addition:a+b->TT::operator+(constT&b)const;因此运算符返回类型为T的非常量。如果我们看这个guideline作者说返回类型应该是const以避免以下语法:(a+b)=c现在假设我不介意这种语法,并考虑a和b是大数组。从“纯”性能的角度来看,返回类型中缺少const关键字是否会阻止编译器的优化(g++和带有-O3的英特尔icpc)?如果答案是"is",为什么? 最佳答案 这是一个有趣的问题。在C++03中,没有更好的机会使用这两

c++ - Const temporary from template type 以及为什么使用 std::add_const?

以下代码摘自cppreference.com.#include#includestructfoo{voidm(){std::coutvoidcall_m(){T().m();}intmain(){call_m();call_m::type>();}但是,当使用VC++Nov2012CTP编译时,输出为Non-cvNon-cv而不是预期的:Non-cvConst另外,下面两个语句有什么区别:call_m();和call_m::type>(); 最佳答案 这似乎是MSVC的一个错误。使用T()形式的表达式(就标准而言,这是一种显式类型转

C++11/14 : How to remove a pointer-to-member from a type?

在C++11或C++1y/14中:给定一个TC::*形式的指向成员类型的值,我想得到指向类型例如:#includeusingnamespacestd;structT1{staticvoidf(){coutstructV;templatestructV{staticconstexprT1U1::*pm=&U1::x;};templatestructV{staticconstexprT2U2::*pm=&U2::x;};templatevoidf(Wpm){typedef???T;T::f();}intmain(){f(V::pm);f(V::pm);}有没有办法做到这一点?上面的???是

c++ - CTRP 派生类中没有名为 'type' 的类型

我一直在试验CuriouslyRecurringTemplatePattern对于通用的单参数仿函数,有两种实现:一种使用有效的模板模板参数,另一种我尝试在接口(interface)类中访问派生的Functor::type。在后一个示例中,编译器(gcc5.4.0)报告error:notypenamed'type'in'structCube'templateclassFunctor>classFunctorInterface_1{private:constFunctor&f_cref;public:FunctorInterface_1():f_cref(static_cast&>(*t

c++ - 错误 : Expression must have integral or unscoped enum type

#include#include#include#include#includeusingnamespacestd;intmain(){floatsize;floatsumNum=0;floatmaxNum,minNum;floatmean;floattotalDev=0;floatdevSqr=0;floatstdDev;//Createauserinputsizestd::cout>size;float*temp=newfloat[size];//Gettinginputfromtheuserfor(intx=1;x>temp[x];}//Outputofthenumbersins

c++ - 错误 : Qualifiers dropped in binding reference of type x to initializer of type y

为什么下面会抛出这个错误:IntelliSense:qualifiersdroppedinbindingreferenceoftype"string&"toinitializeroftype"conststring".hclassA{public:wstring&GetTitle()const;private:wstringtitle;};.cppwstring&GetTitle()const{returnthis->title;}如果我删除const词,它就会停止提示,但我从未对变量进行任何更改? 最佳答案 通过返回对类成员的非c