删除所有左值转换运算符
Deleting all lvalue conversion operators
我想避免从一种类型到另一种类型的所有左值转换:
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;
}
这可能吗?
我需要如何以及删除哪些转换运算符?
示例 godbolt
你可能会
struct T
{
A a;
operator A() { return a; }
template <typename T> operator const T&() = delete;
};
我想避免从一种类型到另一种类型的所有左值转换:
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;
}
这可能吗? 我需要如何以及删除哪些转换运算符?
示例 godbolt
你可能会
struct T
{
A a;
operator A() { return a; }
template <typename T> operator const T&() = delete;
};