草庐IT

const-reference

全部标签

c++ - const 关键字在函数声明中的位置

这个问题在这里已经有了答案:关闭9年前。PossibleDuplicate:DifferencebetweenconstdeclarationsinC++#includeclassBar{};voidfoo(constBarx){}//l5voidfoo(Barx){}//l6voidfoo(Barconstx){}//l7////pointerfunctionsvoidfoo(constBar*x){}//l11voidfoo(Bar*x){}//l12voidfoo(Bar*constx){}//l13编译器输出:(长话短说l5,l6,l7冲突;但只有l12,l13冲突)untit

c++ - 为什么 const 右值限定的 std::optional::value() 返回一个 const 右值引用?

std::optional::value()有以下两个重载constexprT&value()&;constexprconstT&value()const&;constexprT&&value()&&;constexprconstT&&value()const&&;返回const右值引用有什么意义?我能想到的唯一原因是让编译器能够帮助捕获以下(真的很奇怪)情况下的未定义行为autor=std::cref(const_cast&&>(std::optional{}).value());如果std::optional::value()返回了一个constT&,那么上面的代码将编译并在时导致

c++ - 使用静态常量初始化 unique_ptr 时出现 undefined reference 错误

当我尝试使用staticconst来初始化unique_ptr时,我收到“undefinedreference”错误。然而,当我新建一个使用相同常量的指针时,符号似乎被神奇地定义了。这是一个重现错误的简单程序:Outside_library.hclassOutside_library{public:staticconstintmy_const=100;};main.cpp#include"Outside_library.h"#include#includeclassMy_class{public:My_class(intnum){m_num=num;};virtual~My_class

c++ - 变量上的 `const constexpr` 是多余的吗?

cppreference指出:Aconstexprspecifierusedinanobjectdeclarationornon-staticmemberfunction(untilC++14)impliesconst.“对象声明”是指“任何变量声明”吗?即是constexprconstintsomeConstant=3;相当于constexprintsomeConstant=3;在C++11、C++14和C++17中? 最佳答案 在带有原语的声明中,例如您示例中的声明,const确实是多余的。但是,可能会有一些奇怪的情况需要con

c++ - 为什么 static_cast 不使用转换运算符指向 const 的指针?

来self的包装类Pointer我只想返回指向const的指针:Baseconst*.类型转换时Pointer至Derivedconst*我收到一个编译错误:errorC2440:'static_cast':'Pointer'cannotbeconvertedto'constDerived*'(译自德语VS2012)structBase{};structDerived:publicBase{};templateclassPointer{public:Pointer(T*t=nullptr):p(t){}//operatorT*(){returnp;}operatorTconst*()c

c++ - 如何禁止采用 const 引用的函数中的临时变量?

背景:假设我有这样的东西:structitem{intx;item(inty):x(y){}}classitem_view{constitem⁢public:item_view(constitem&it_):it(it_){}friendstd::ostream&operator我不能重载的原因operator就是比较人性化,而且view是用来传递数据给SQL的,所以ticks等一些字符必须转义。问题:有人可能想做这样的事情:autoview=item_view(2);std::cout这似乎是未定义的行为。问题:如何防止构建item_view来自临时工?

c++ - 为什么 T& 类型的模板函数参数可以绑定(bind)到 const 左值而不是右值?

考虑:templatevoidf(T&);constintb=2;f(b);//okf(2);//error,cannotbindrvaluetolvaluereference为什么是f(constint)允许?逻辑似乎表明,如果程序员没有明确定义模板参数为constT&,他/她想修改绑定(bind)变量。所以在这种情况下,问题是,为什么模板实例化在没有明确要求的情况下让自己可以自由地使用consts进行实例化?即使有允许模板实例化用const实例化的理由,那么为什么在那种情况下会禁止绑定(bind)到右值?您可以将右值绑定(bind)到const左值引用。在这种情况下,模板将被实例化

c++ - Const 限定符和前向引用

在seastar中看到过这段代码框架templateclasslambda_taskfinal:publictask{Func_func;public:lambda_task(scheduling_groupsg,constFunc&func):task(sg),_func(func){}lambda_task(scheduling_groupsg,Func&&func):task(sg),_func(std::move(func)){}virtualvoidrun_and_dispose()noexceptoverride{_func();deletethis;}};template

c++ - const 可以,但不是 constexpr?

使用constexpr指定的函数foo_constexpr我有如下所示的代码:constautox=foo_constexpr(y);static_assert(x==0);当x的声明更改为constexpr时,在什么情况下代码可能无法编译?(毕竟,x必须已经是用于static_assert的常量表达式。)即:constexprautox=foo_constexpr(y);static_assert(x==0); 最佳答案 在一般中,当foo_constexpr的执行违反常量表达式的要求时,它可能无法编译。请记住,constexpr

c++ - 将返回 Foo 的函数的结果分配给 const Foo&

我有一个返回Foo类型对象的函数:FoogetFoo();我知道下面的代码可以编译并且可以工作,但我为什么要这样做呢?constFoo&myFoo=getFoo();对我来说,下面的代码更具可读性,并且不会强制我记住C++允许我将右值分配给const引用:constFoomyFoo=getFoo();两者有什么区别?为什么我会使用第一个而不是第二个?为什么我会使用第二个而不是第一个? 最佳答案 与流行观点相反,不能保证将按值返回对象的函数的结果分配给const引用会导致比将其分配给对象本身更少的拷贝。当您将右值分配给const引用时