简体   繁体   English

有没有办法声明该函数的输入参数必须是const reference?

[英]Is there a way to declare that input parameter of the function must be const reference?

motivation for this is that (rarely)I have a need to know that the input parameter of the class constructor or function in general is const. 这样做的动机是(很少)我需要知道类构造函数或函数的输入参数通常是const。 Usually when the class is a helper that "automates" some procedure. 通常,当该类是一个使某些过程“自动化”的助手时。
Example: 例:
Is this the OK scoped way to get random element from the container? 这是从容器中获取随机元素的确定范围的方法吗?
If you look at the code it is clear that if the container passed to constructor is changed later class functionality is broken. 如果您看一下代码,很明显,如果传递给构造函数的容器被更改,则类功能将被破坏。 So is there a way to have a function "demand" const instead of "promise" const. 因此,有一种方法可以使用函数“ demand” const而不是“ promise” const。

Example: 例:

int f(const vector<int>& v)
{
    return v.size();
}
int main()
{
    vector<int> v;
    v.push_back(42); // can f be changed to reject v because it is not const 
    cout << f(v);

}

Declare but do not implement a non-const version. 声明但不实现非const版本。

int f(vector<int>& v);

Attempts to pass a non-const vector will be resolved to this function, and then you will get a linker error because no such function exists. 尝试通过非常量向量的尝试将被解析为该函数,然后您将收到链接器错误,因为不存在此类函数。

Some fancy template games may be able to turn it into a compile-time error. 一些精美的模板游戏可能能够将其转换为编译时错误。

There is no way to guarantee that. 没有办法保证这一点。 If you want to be sure the object doesn't change, it should be owned by the class. 如果要确保对象不变,则该对象应归类所有。 One way to accomplish that is to copy the object you want to keep constant into the class, or if using C++11 to let the constructor take an rvalue-reference, and move the object. 一种实现方法是将要保持常数的对象复制到类中,或者使用C ++ 11让构造函数获取右值引用并移动对象。

If you want some attributes inside class be safe from any external changes they should be copied by constructor to attribute const TypeName _attr; 如果希望类中的某些属性不受任何外部更改的影响,则应将它们由构造const TypeName _attr;复制到属性const TypeName _attr; .

I don't think, it is possible. 我不认为有可能。

If it would be possible, you could still do like that 如果有可能,您仍然可以那样做

Object mutable_;
const Object & notMutavle = mutable_;
func(notMutable);
mutable.change();

Anyway, even const object's state can be possibly changed, if it has some mutable feilds. 无论如何,即使const对象的状态也可以更改,如果它具有一些mutable字段。

If you want to be sure, that nobody changes your object, you should manage it yourself. 如果要确保没有人更改对象,则应该自己进行管理。

You can do it by overloading the function for non-const reference. 您可以通过重载非常量引用的函数来实现。

Next example demonstrates this. 下一个示例对此进行了演示。 However, instead of returning -1, the first version of f should throw an exception. 但是,代替返回-1,f的第一个版本应该引发异常。

#include <vector>
#include <iostream>

int f(std::vector<int>& )
{
    return -1;
}
int f(const std::vector<int>& v)
{
    return v.size();
}
int main()
{
    std::vector<int> v;
    v.push_back(42); // can f be changed to reject v because it is not const
    std::cout << f(v)<<std::endl;

    const std::vector<int> cv(5);
    std::cout << f(cv)<<std::endl;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在C ++中,为什么我们在重载函数中将引用参数声明为const? - in c++ why we declare reference parameter as const in overloaded function? 如何声明对函数类型的const引用 - How to declare a const reference to function type 在什么情况下你会声明一个非引用的非指针类型函数参数const? - Under what scenarios would you declare a non-reference, non-pointer type function parameter const? lambda 函数中的常量引用参数 - Const reference parameter in lambda function const引用模板参数中的函数? - const reference to a function in a template parameter? 引用和常量引用作为函数参数的区别? - Difference between reference and const reference as function parameter? function 上下文中 const 引用参数和 const 参数之间的区别 - Difference between a const reference parameter and const parameter in the context of a function 有没有办法在我的通用功能路由器设计中支持“const 引用”作为功能签名参数? - Is there a way to support 'const reference' as a function signature parameter in my generic function router design? const引用函数参数的地址何时唯一? - When is the address of a const reference function parameter unique? 函数指针引用参数转换为const - Function pointer reference parameter conversion to const
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM