首页 > TAG信息列表 > const-reference

c-具有已选择具有非const ref的复制构造函数的参数类型的函数?

前段时间,当我想编写is_callable< F,Args ...>时,我对某些代码的以下行为感到困惑.特征.重载解析不会调用非const ref接受参数的函数,对吗?为什么在下面它不拒绝,因为构造函数想要一个Test&amp ;?我希望它取f(int)! struct Test { Test() { } // I want Test not be copyable fr

c – 为什么`const T&`不确定是const?

template<typename T> void f(T a, const T& b) { ++a; // ok ++b; // also ok! } template<typename T> void g(T n) { f<T>(n, n); } int main() { int n{}; g<int&>(n); } 请注意:b是const T&和B是好的! 为什么const T&am

c – const引用类成员是否延长了临时生命?

为什么这样: #include <string> #include <iostream> using namespace std; class Sandbox { public: Sandbox(const string& n) : member(n) {} const string& member; }; int main() { Sandbox sandbox(string("four")); cout

c – 为什么不调用带有const引用返回值的重载方法?

考虑以下代码: #include <iostream> using namespace std; class A { private: int x; public: int& get_ref() { cerr << "non const" << endl; return x; } const int& get_ref() const {

c – 是否可以更改临时对象并将其作为参数传递?

是否可以更改临时对象并将其作为参数传递? struct Foo { Foo& ref() { return *this; } Foo& operator--() { /*do something*/; return *this; } // another members }; Foo getfoo() { return Foo(); } // return Foo() for example or something else void func_va

这个有效的C代码是否符合标准?

我有这个示例代码: struct A { bool test() const { return false; } }; template <typename T = A> class Test { public: Test(const T& t = T()) : t_(t){} void f() { if(t_.test()) { //Do something

C:const引用和初始化顺序

我想知道我是否正在使用以下的好方法: >我想构造一个父类(A类),这个类应该拥有一个给定“Foo”类的实例 >我希望父类拥有子类成员(类B),并且该成员应该具有对父类的foo成员的引用. 下面的代码似乎有效,但我想知道我是否只是“幸运”编译器足够同情. 为清楚起见,我在下面的评论中添加