英文:
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 <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));
}
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("0123");
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 &&
-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 &&
-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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论