防止 prvalue 被绑定到变量。

huangapple go评论147阅读模式
英文:

Prevent prvalue from being bound to a variable

问题

我有一个函数,它返回一个临时对象,该对象内部持有一个引用,其唯一目的是允许一种返回类型重载。例如:

#include <iostream>
#include <vector>

class MyData {
private:
    class IntOrChar {
    public:
        IntOrChar(char const& c) : c_(c) {}
        operator char() { return c_; }
        operator int() { return c_ - '0'; }
    private:
        char const& c_;
    };

public:
    MyData(std::string number) : number_(number) {}

    IntOrChar get(std::size_t i) { return number_[i]; }

private:
    std::string number_;
};

void accepts_char(char c) { std::cout << "char: " << c << '\n'; }
void accepts_int(int i) { std::cout << "int: " << i << '\n'; }

int main(){
    MyData n("012345");
    accepts_char(n.get(3));
    accepts_int(n.get(3));
}

实际代码更复杂,引用是指向一些内部状态的。

使用上述示例,我可以阻止从get()返回的值被保持吗?我想防止这样的代码:

MyData n("0123");
auto r = n.get(2);
... // 在代码的后面很久
accepts_char(r);

因为在使用它的地方,内部引用可能不再有效。我想强制转换类(IntOrChar)立即转换为具体类型,而不是保持它。

我认为我要找的术语是我想防止prvalue被转换为xvalue?

我只是想防止意外发生这种情况。我不介意是否可以通过某种类型的强制转换或其他巫术来绕过它。

英文:

I have a function that returns a temporary object which holds a reference inside and its sole purpose is to allow a sort of return-type overloading. For example:

#include &lt;iostream&gt;
#include &lt;vector&gt;

class MyData {
private:
    class IntOrChar {
    public:
        IntOrChar(char const&amp; c) : c_(c) {}
        operator char() { return c_; }
        operator int() { return c_-&#39;0&#39;; }
    private:
        char const&amp; c_;
    };

public:
    MyData(std::string number) : number_(number) {}

    IntOrChar get(std::size_t i) { return number_[i]; }

private:
    std::string number_;
};

void accepts_char(char c) { std::cout &lt;&lt; &quot;char: &quot; &lt;&lt; c &lt;&lt; &#39;\n&#39;; }
void accepts_int(int i) { std::cout &lt;&lt; &quot;int: &quot; &lt;&lt; i &lt;&lt; &#39;\n&#39;; }

int main(){
    MyData n(&quot;012345&quot;);
    accepts_char(n.get(3));
    accepts_int(n.get(3));
}

The actual code is more complex, and the reference is to some internal state.

Using the above example, can I prevent the value returned from get() from being held on to? I want to prevent code like this:

MyData n(&quot;0123&quot;);
auto r = n.get(2);
... // much later in the code
accepts_char(r);

since at the point where it is being used, the internal reference may no longer be valid. I want to force the casting class (IntOrChar) to be cast to a concrete type immediately and not held on to.

I think the terms I am looking for are that I want to prevent the prvalue from being turned into an xvalue?

I just want to prevent this from being done by accident. I don't care if it can be gotten around using some type of force-casting or other wizardry.

答案1

得分: 4

A workaround is to make the conversion functions &amp;&amp;-qualified. Anyone who asks why they’re having to write accepts_char(std::move(r)) can be reminded of the purpose of the type.

The real answer is to mark the type lifetime-bound, but that’s very much still a proposal. Another possibility for the exact example given would be to make auto fail for the type, but that’s also merely a proposal.

英文:

A workaround is to make the conversion functions &amp;&amp;-qualified. Anyone who asks why they’re having to write accepts_char(std::move(r)) can be reminded of the purpose of the type.

The real answer is to mark the type lifetime-bound, but that’s very much still a proposal. Another possibility for the exact example given would be to make auto fail for the type, but that’s also merely a proposal.

huangapple
  • 本文由 发表于 2023年7月23日 21:51:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76748585.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定