Nat*_*son 29 c++ type-conversion
假设我有一个简单的Duration类:
class Duration
{
int seconds;
public:
Duration(int t_seconds) : seconds(t_seconds) { }
};
int main()
{
Duration t(30);
t = 60;
}
Run Code Online (Sandbox Code Playgroud)
我决定我不喜欢能够从 隐式转换int为Duration。我可以制作构造函数explicit:
class Duration
{
int seconds;
public:
explicit Duration(int t_seconds) : seconds(t_seconds) { }
};
int main()
{
Duration t(30); // This is fine, conversion is explicit
t = 60; // Doesn't compile: implicit conversion no longer present for operator=
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我不想立即破坏所有隐式转换为 的调用代码怎么办Duration?我想要的是这样的:
class Duration
{
int seconds;
public:
[[deprecated]]
Duration(int t_seconds) : seconds(t_seconds) { }
explicit Duration(int t_seconds) : seconds(t_seconds) { }
};
int main()
{
Duration t(30); // Compiles, no warnings, uses explicit constructor
t = 60; // Compiles but emits a deprecation warning because it uses implicit conversion
}
Run Code Online (Sandbox Code Playgroud)
这将允许现有代码在编译时识别当前依赖隐式转换的任何位置,因此可以将它们重写为使用显式转换(如果需要)或重写为具有正确的行为(如果不需要)。
然而这是不可能的,因为我不能Duration::Duration(int)超载Duration::Duration(int)。
有没有一种方法可以实现类似这种效果的效果,而不是“使转换明确,接受调用代码在编写适当的更改之前不会编译”?
康桓瑋*_*康桓瑋 34
您可以变成Duration(int t_seconds)一个可以接受int并将其设置为已弃用的模板函数。
#include<concepts>
class Duration {
int seconds;
public:
template<std::same_as<int> T>
[[deprecated("uses implicit conversion")]]
Duration(T t_seconds) : Duration(t_seconds) { }
explicit Duration(int t_seconds) : seconds(t_seconds) { }
};
Run Code Online (Sandbox Code Playgroud)
如果允许t = 0.6,只需将 更改same_as为convertible_to。
| 归档时间: |
|
| 查看次数: |
1589 次 |
| 最近记录: |