enable_if 和转换运算符?
有机会将 enable_if
与类型转换运算符一起使用吗?看起来很棘手,因为返回类型和参数列表都是隐式的。
Any chance to use enable_if
with a type conversion operator? Seems tricky, since both return type and parameters list are implicit.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据我所做的一点研究(并忽略 Johannes 的 c++0x 注释),我的答案是,这取决于您想要
enable_if
的用途。如果您希望从类型T
到T
的转换操作存在或不存在,那么答案似乎是否定的,在 C++03 中没有办法(如乌戈说)。但是,如果您需要enable_if
根据T
的类型更改运算符的行为,那么是的,有一个解决方法是调用启用的辅助函数(如 Matthieu 建议的那样称为to
)。根据记录,我对此感到沮丧好几天,直到我意识到我想要
enable_if
不是为了 SFINAE,而是为了编译时行为更改。您可能还会发现这也是您需要enable_if
的真正原因。只是一个建议。(请注意,这是C++98时代的答案)
From the little research I did (and ignoring the c++0x comment from Johannes), my answer is that it depends what you want the
enable_if
for. If you want the conversion operation toT
to exist or not from the typeT
then it seems that the answer is no, there is no way in C++03 (as Ugo said). But if you need theenable_if
to change the behavior of the operator depending on the type ofT
then yes, there is a workaround which is to call an enabled helper function (calledto<T>
as Matthieu suggested).For the record, I was frustrated with this for several days, until I realized that I wanted
enable_if
not for SFINAE but for compile-time behavior change. You may also find that this is the real reason for your need forenable_if
also. Just a suggestion.(Please note that this is an answer for the C++98 era)
删除文档:
似乎没有办法为转换运算符指定启用程序。但是,转换构造函数可以将启动器作为额外的默认参数。
dixit the documentation:
There does not seem to be a way to specify an enabler for a conversion operator. Converting constructors, however, can have enablers as extra default arguments.
虽然我可以理解这个问题的理论意义,但我个人尽可能避免使用转换运算符。
我唯一一致使用的是转换为伪布尔值(使用 Safe Bool 习语),用于智能指针或代理,并且如上所述,我使用一种技巧来实际阻止完整的布尔语义...
如果我如果想要促进转换,我更喜欢这样的东西:
它不受转换运算符(在签名方面)的限制,并且需要显式调用,只是因为它更清晰一些。
While I can understand the theoritecal interest in the question, I personally refrain from using conversion operators as much as possible.
The only one I ever use with consistence is the conversion to a pseudo-boolean (using the Safe Bool idiom), for smart-pointers or proxies, and as noted I use a trick to actually prevent the full boolean semantic...
If I ever want to facilitate conversions, I much prefer something along the line of:
which does not suffer from the limitations (in term of signature) of the conversion operator and requires explicit invocation, just because it's a bit clearer.
其实我已经找到办法了;我们使用私有的、未使用的类来标记不应该存在的转换,并使用 boost::mpl::if_ 来选择是否生成 NoConversion 或所需类型的转换。
Actually, I have found a way; we use a private, unused class to mark a conversion that shouldn't exist, and we use
boost::mpl::if_
to select whether to produce a conversion to NoConversion, or to the desired type.