非常量对象的常量向量
在接口中定义函数时:
virtual void ModifyPreComputedCoeffs ( std::vector < IndexCoeffPair_t > & model_ ) = 0;
我们希望指定向量 model_ 不应更改,即不应在向量上执行 Push_back 等操作,但 model_ 中的 IndexCoeffPair_t 结构对象可以更改。 我们应该如何指定呢?
virtual void ModifyPreComputedCoeffs ( const std::vector < IndexCoeffPair_t > & model_ ) = 0;
我认为不起作用。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不要将向量传递到函数中,而是执行标准库所做的操作并传递一对迭代器。
Rather than passing the vector into the function, do what the standard library does and pass a pair of iterators instead.
在我看来,C++ 常量正确性概念被高估了。您刚刚发现的是它的一大局限性:它不能按组合进行扩展。
为了能够创建非常量对象的 const 向量,您需要实现自己的向量类型。请注意,例如,即使标准库也必须为 const_iterator 引入新类型。
我的建议是在被迫的地方使用常量正确性,而不是在任何可以使用的地方。理论上,const 正确性应该对程序员有帮助,但由于语法原因,成本非常高,而且非常原始(只有一位,不能通过组合进行扩展,甚至需要代码重复)。
另外,根据我的经验,这个所谓的大帮助实际上并不是那么大......它捕获的大多数错误都与常量正确性机制本身有关,而不是与程序逻辑有关。
有没有想过为什么大多数语言(包括 C++ 之后设计的语言)没有实现这个想法?
The C++ const-correctness concept is IMO way overrated. What you just discovered is one of the big limitations it has: it doesn't scale by composition.
To be able to create a const vector of non-const objects you need to implement your own vector type. Note that for example even the standard library had to introduce new types for const_iterators.
My suggestion is to use const-correctness where you are forced to, and not everywhere you can. In theory const correctness should help programmers, but comes at a very high cost because of the syntax and is very primitive (just one bit, doesn't scale by composition, even requires code duplication).
Also in my experience this alleged big help is not really that big... most of the errors it catches are related to the const-correctness machinery itself and not to program logic.
Ever wondered why most languages (including ones designed after C++) didn't implement this idea?
这可能在 C++14 中为 std::dynarray。
实际上,如果大小在编译时固定,您可以使用 std::array。但它可能更适合嵌入式编程、缓冲区、矩阵等,因为您通常直到运行时才知道所需的大小,或者您希望它是可配置的。
This is likely to be in C++14 as std::dynarray.
Actually if the size is fixed at compile time you can use std::array. But it's probably more use for things like embedded programming, buffers, matrices and so on as often you don't know the desired size until runtime or you want it to be configurable.
如果您能够修改 IndexCoeffPair_t,您可以添加一些 const 成员函数,并使用它们通过使用 mutable 关键字使成员可变来更改其某些成员。但这是一种黑客行为,因为您现在可以更改任何 const IndexCoeffPair_t 的内容。
例子:
If you are able to modify
IndexCoeffPair_t
, you could add some const member functions and use them to change some of its members by making the members mutable using the mutable keyword. This is kind of a hack though, since you would now be able to change the contents of anyconst IndexCoeffPair_t
.Example:
这是 MahlerFive 答案的通用版本:
然后您可以使用 std::vector> const 在你的代码中,它基本上会按预期运行。
Here's a generic version of MahlerFive's answer:
You can then use
std::vector<Mutable<T>> const
in your code, which will mostly behave as intended.您可以尝试创建
const std::vector
。那么你不能改变向量,但你可以改变向量内的对象。但要准确,因为您将修改原始对象而不是副本。
使用智能指针或原始指针取决于您的用例:您拥有拥有向量或只是观察者向量。
You can try to create
const std::vector<YouType*>
. Then you can't change the vector but you can change objects inside vector.But be accurate because you will modify original objects not copies.
Use smart pointers or raw pointers depends on your use cases: you have owning vector or just vector of observers.