C++转换 const 按引用传递
给定模板传递引用转换/类型转换运算符(不带 const)是可能的:
class TestA
{
public:
//Needs to be a const return
template<typename TemplateItem>
operator TemplateItem&() const {TemplateItem A; A = 10; return A;}
};
int main()
{
TestA A;
{
int N;
N = A;
printf("%d!\n",N);
}
{
float N;
N = A;
printf("%f!\n",N);
}
return 0;
}
并给定以下代码(带 const):
class TestA
{
public:
//Produces error
template<typename TemplateItem>
operator const TemplateItem&() const {TemplateItem A; A = 10; return A;}
};
产生这些错误:
错误:无法在赋值中将“TestA”转换为“int”
错误:无法在赋值中将“TestA”转换为“float”
问题
如何使转换/类型转换运算符返回模板类型的 const 传递引用?
上下文
在大多数人进来并担心“你不能将其转换为任何东西”之前,您需要上下文。上面的代码是伪代码 - 我只对 const 引用返回是否可能感兴趣,而不是模板化转换函数的陷阱。但如果您想知道它的用途,它相对简单:
TemplateClass ->转换(转成字节数据)->文件
TemplateClass <- 转换(从字节数据改回) <- 文件
用户应该知道他们将得到什么,或者希望它是自动化的(IE 保存/加载状态)。是的,模板有一种通用方法,使用指针将任何类型转换为字节数据。
不要让我夸夸其谈 std 已经在做这种事情了。转换过程是更复杂的类库设置的一部分。
我是一名程序员。相信我。 C++ 信任我并允许我犯错误。我学习的唯一方法。
Given a template pass-by-reference conversion/type-cast operator (without const) is possible:
class TestA
{
public:
//Needs to be a const return
template<typename TemplateItem>
operator TemplateItem&() const {TemplateItem A; A = 10; return A;}
};
int main()
{
TestA A;
{
int N;
N = A;
printf("%d!\n",N);
}
{
float N;
N = A;
printf("%f!\n",N);
}
return 0;
}
And given the following code (with const):
class TestA
{
public:
//Produces error
template<typename TemplateItem>
operator const TemplateItem&() const {TemplateItem A; A = 10; return A;}
};
Produces these errors:
error: cannot convert 'TestA' to 'int' in assignment
error: cannot convert 'TestA' to 'float' in assignment
Question
How do I make it so the conversion/type-cast operator return a const pass-by-reference of the template type?
Context
Before most people come in and freak about how 'you can't convert it to just anything', you'll need context. The above code is pseudo code - I'm only interested on const reference returns being possible, not the pitfalls of a templated conversion function. But if you're wondering what it's for, it's relatively simple:
TemplateClass -> Conversion (turned into byte data) -> File
TemplateClass <- Conversion (changed back from byte data) <- File
The user is expected to know what they are getting out, or it's expected to be automated (I.E. saving/loading states). And yes, there is a universal method for templates using pointers to convert any type into byte data.
And don't give me claptrap about std doing this sort of thing already. The conversion process is part of a more complicated class library setup.
I'm a programmer. Trust me. C++ trusts me and lets me make mistakes. Only way I'll learn.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您的转换运算符已经是未定义的行为,因为您返回了对超出范围的局部变量的引用(const 或非 const)。如果您将转换运算符更改为按值返回,那么它应该可以正常工作,而不会引起 UB。
编辑:(删除了有关转换运算符的不正确信息)。
但是您真的确定您真的希望您的类类型可以转换为任何类型吗?当您维护代码并且它会自动转换为意外类型时,这似乎只会在将来引起许多麻烦。
另一种可能的实现是创建一个
as
模板方法,该方法基本上执行转换运算符想要执行的操作,并像obj.as()
一样调用它。Firstly, your conversion operator is already undefined behavior because you return a reference (const or not) to a local variable that has gone out of scope. It should work fine if you change your conversion operator to return by value which won't induce UB.
EDIT: (removed incorrect information about conversion operators).
But are you really sure that you really want your class type to be convertible to anything? That seems like it's just going to cause many headaches in the future when you're maintaining the code and it converts to an unexpected type automatically.
Another possible implementation is to create an
as
template method that basically does what your conversion operator wants to do and call it likeobj.as<int>()
.