问题
I want to avoid all lvalue conversions from a type to another type:
struct A
{};
struct T
{
A a;
operator A() { return a; }
//operator A&() = delete; how to delete lvalue conversions to A?
};
void bar(A)
{}
void foo(const A&)
{}
void foo2(A&)
{}
int main()
{
T t;
bar(t); // fine
foo(t); // should never convert to ref-to-const A
foo2(t); // should never convert to ref-to A
return 0;
}
Is this possible? How and which conversion operators do I need to delete?
Example on godbolt
回答1:
You might do
struct T
{
A a;
operator A() { return a; }
template <typename T> operator const T&() = delete;
};
Demo
来源:https://stackoverflow.com/questions/66153505/deleting-all-lvalue-conversion-operators