草庐IT

template_const

全部标签

c++ - 'const' 变量是否默认在 C++ 中预先计算?

假设我有像这样的位置变量constfloatlatitude=51.+11./60.+33.0461/3600.;constfloatlongitude=12.+50./60.+31.9369/3600.;并在程序中经常使用它们。编译器会预先计算吗?(这个例子应该不会产生太多的开销,但你明白了。)指出位置的奖励积分。;)TIA 最佳答案 我认为编译器通常不需要计算算术常量表达式的结果。编译器是,但是,需要计算整数常量表达式的结果(基本上,常量表达式仅由整数和其他值转换为整数组成)在需要结果的情况下——也就是说,当整数常量表达式用作数

C++: 类方法前的 "const"

以这个方法声明为例:constVectorVector::operator-(constVector&other)const;我知道第二个const使作为参数传递的Vector不可变,最后一个const声明该方法不会更改Vector类的当前实例....但是第一个const到底意味着什么或导致什么? 最佳答案 这是一种过时的安全措施,可以防止像a-b=c这样的无意义代码被编译。(我说“过时”是因为它阻止了movesemantics,它只适用于非常量右值。) 关于C++:类方法前的"cons

C++ : friend declaration ‘declares a non-template function

我在重载时遇到问题流运算符(operator),我找不到解决方案:templateclassNVector{inlinefriendstd::ostream&operator&rhs);};templateinlinestd::ostream&NVector::operator&rhs){/*SOMETHING*/returnlhs;};它产生以下错误信息:warning:frienddeclaration‘std::ostream&operatorerror:‘std::ostream&NVector::operator如何解决这个问题?非常感谢。 最佳答

c++ - 在类内部定义一个const静态对象变量

我需要在类定义中创建一个静态对象。这在Java中是可能的,但在C++中我得到一个错误:../PlaceID.h:9:43:error:invaliduseofincompletetype‘classPlaceID’../PlaceID.h:3:7:error:forwarddeclarationof‘classPlaceID’../PlaceID.h:9:43:error:invalidin-classinitializationofstaticdata我的类(class)是这样的:#includeclassPlaceID{public:inlinePlaceID(conststd::

c++ - 强制偏好 const 版本?

假设我有classA{doublea;doubleValue()const{returna;}double&Value(){returna;}}//later:Afoo;doubleb=foo.Value();现在,将调用非常量版本。有没有一种强制使用const版本的好方法?我认为使用类型转换是可能的,但我认为这不是很优雅。 最佳答案 您可以将其转换为const。doubleb=static_cast(foo).Value();(我不认为我曾经显式地将const添加到变量中。我不确定static_cast是否比const_cast.

c++ - 错误 : expected primary-expression before ‘>’ : templated function that try to uses a template method of the class for which is templated

这个问题在这里已经有了答案:WhereandwhydoIhavetoputthe"template"and"typename"keywords?(8个答案)关闭8年前。在使用模板和仿函数(未出现在这个问题中)时,我最终遇到了以下简化的问题。以下代码(也可用here)classA{public:templateboolisGood(intin)const{constTf;returninbooltryEvaluator(T&evaluator,intvalue){returnevaluator.isGood(value);}intmain(intargc,constchar*argv[]

c++ - 在 C++ 中,使用 #define 还是 const 来避免魔数(Magic Number)更好?

使用#define优于const(反之亦然)有哪些优点和缺点?当我读到有关糟糕的编程实践(尤其是魔数(MagicNumber))时,我发现自己更频繁地使用#define。一些问题突然出现在我的脑海中,例如:大量使用#define不好吗?是否占用内存空间?使用const会更快吗?我读了一些关于这个的内容,但我仍然不确定,据我所知:#define定义了一个宏(不确定宏是什么意思),它处理预处理。在处理代码之前,它将已定义关键字的所有实例替换为其他内容。另一方面,const是变量,其值不能在运行时中途更改。我能想到使用const的唯一原因是该值是否依赖于其他变量。例如:#definePI3.

C++ 如何使 template<T>f() 为整数 T 返回 -1,为指针类型返回 nullptr

我需要完成以下任务:templatef(){:return{-1ifTisofintegraltype,elsenullptr}}在我的特定用例中,T可以是四种类型之一:intPy_ssize_t//ssize_tPy_hash_t//ssize_tPyObject*//PyObjectissomeCstruct这是我迄今为止最好的解决方案:templateTtest(typenameenable_if::value,void*>::type=nullptr){return-1;}templateTtest(typenameenable_if::value,void*>::type=n

constexpr链接列表 - 从const x*到x*的无效转换

这是我创建一个简单的constexpr链接列表的尝试-structNode{constexprNode(constintn,Nodeconst*next=nullptr):value(n),next(next){}constexprNodepush(constintn)const{returnNode(n,this);}intvalue;Nodeconst*next;};constexprautogetSum(Noden){intsum=0;Node*current=&n;while(current!=nullptr){sum+=current->value;current=current->

c++ - const char * 值生命周期

这个问题在这里已经有了答案:Canalocalvariable'smemorybeaccessedoutsideitsscope?(20个答案)关闭7年前。编辑:重复标记中的链接问题已经回答了为什么这个问题中的代码有效的问题。关于字符串文字生命周期的问题在这个问题的答案中得到了回答。我试图了解constchar*指向的字符串如何以及何时被释放。考虑:constchar**p=nullptr;{constchar*t="test";p=&t;}cout离开内部范围后,我希望p成为指向constchar*的悬空指针。但是在我的测试中它不是。这意味着即使在t超出范围之后,t的值实际上仍然有效