如何禁止赋值不引用变量?(how to forbid assignment to not reference variables?)

编程入门 行业动态 更新时间:2024-10-27 04:34:25
如何禁止赋值不引用变量?(how to forbid assignment to not reference variables?)

我担心这是一个愚蠢的问题,但......

有人可以建议我强制从函数(或方法)返回一个返回值,该值返回对内部静态变量或类/结构成员的引用 ,仅分配给引用变量?

我试着用一个简单的例子来解释我的愿望。

给定以下代码,使用返回对内部静态变量的引用的函数wrapValue()

int & wrapValue (int v0) { static int val; return val = v0; } int main () { // how to permit this ... int & v0 { wrapValue(0) }; // ... but forbid this ... int v1 { wrapValue(1) }; int v2; // ... and this ? v2 = wrapValue(2); }

有一种方法可以允许初始化v0 (并将v0绑定到静态变量),并禁止初始化v1和赋值v2 (不将v1和v2绑定到静态变量)?

如果使用当前的C ++标准是不可能的,我担心,有人可以建议我另一种方式(但不是太复杂:我打算在我想保持简单的库中使用它)禁止无限制的赋值?

I fear it's a dumb question but...

Someone can suggest me a way to force that a return value from a function (or a method), that return a reference to an internal static variable or a member of the class/struct, is assigned only to reference variables ?

I try to explain what I desire with a minimal example.

Given the following code, with a function wrapValue() that return a reference to the internal static variable,

int & wrapValue (int v0) { static int val; return val = v0; } int main () { // how to permit this ... int & v0 { wrapValue(0) }; // ... but forbid this ... int v1 { wrapValue(1) }; int v2; // ... and this ? v2 = wrapValue(2); }

there is a way to permit the initialization of v0 (and bound v0 to the static variable) and forbid the initialization of v1 and the assignment of v2 (without bounding v1 and v2 to the static variable) ?

And if it's impossible with the current C++ standard, as I fear, someone can suggest me an alternative way (but not too complex: I intend use it in a library that I want to maintain simple) to forbid an unbounded assignment ?

最满意答案

这个解决方案有点棘手,但它的工作原理(我认为)如你所料:

#include <iostream> struct int_wrapper { int value; int_wrapper &operator=(int value) { this->value = value; return *this; } operator int&() { return value; } operator int() { return value; } }; int_wrapper& wrapValue (int v0) { static int_wrapper val; return val = v0; } int main () { // how to permit this ... int & v0 = wrapValue(0); // ... but forbid this ... //int v1 { wrapValue(1) }; // call ambigious int v2; (void)v0; (void)v2; // ... and this ? //v2 = wrapValue(2); // call ambigious }

[现场演示]

This solution is somewhat tricky but it works (I think) as you expect:

#include <iostream> struct int_wrapper { int value; int_wrapper &operator=(int value) { this->value = value; return *this; } operator int&() { return value; } operator int() { return value; } }; int_wrapper& wrapValue (int v0) { static int_wrapper val; return val = v0; } int main () { // how to permit this ... int & v0 = wrapValue(0); // ... but forbid this ... //int v1 { wrapValue(1) }; // call ambigious int v2; (void)v0; (void)v2; // ... and this ? //v2 = wrapValue(2); // call ambigious }

[live demo]

更多推荐

本文发布于:2023-07-24 00:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1239321.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:赋值   变量   forbid   variables   reference

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!