自定义类型 range/value 检查编译时,实时
Custom type range/value check compile-time, real-time
当我们定义一个太大而无法解析的内置类型变量时,Compiler/IDE(我使用的是Visual Studio 2019)会立即警告超出范围,甚至在之前编译项目 - 如果我们定义整数,则“整数常量太大”。
int sum = 100000000000000000000; // E0023, "integer constant is too large"
是否有可能在 c++/VS 中实现对用户定义类型的类似类型检查(我不确定这是否纯粹是 IDE 约束,可能是 - 我是 c++ 的新手)?
在某些情况下会有助于说明超出范围,例如
MonthDay day = 32; // error, "MonthDay literal can't be greater than 31."
Weight applesWeight = 2900000_kg; // "Item too heavy." Yet better if it could also work with custom literals.
来自 constexpr constructor with compile time validation 的版本使用 constexpr
,但这需要变量声明中的关键字。
class A
{
public:
constexpr A(int i) : i(i != 42 ? throw 42 : i) {}
private:
int i;
};
// usage
constexpr A ab = 43; // error, "Expression must have a constant value"
constexpr A abc = 42; // ok
=== 结果 ===
如Lev.M。在评论中提到这种类型的快速 'pre-compile-time' 分析是通过插件完成的(有时称为 linter,并且它们比其他静态分析器做更多的基本检查 - link 下面)并且显然 VC++ 2019 内置了一个新的 linter。这就是在键入代码时检查本机类型溢出的地方,而且此时似乎没有办法为 VC++ 扩展它(对于 C# 已经有一段时间了,如果你熟悉的话) Visual Studio、Roslyn 等)我想知道新的 VC++ linter 是否可以以某种方式扩展,但目前看来还没有。
将@cigien 的 post 标记为答案。
https://devblogs.microsoft.com/cppblog/intellisense-code-linter-for-cpp/
您可以在构造函数中添加检查参数是否在范围内,然后断言:
struct MonthDay
{
constexpr MonthDay (int data) : data(data)
{
if (data > 31)
{
assert(false); // months can't have more than 31 days
}
}
private:
int data;
};
只要构造函数是 constexpr
,它就可以很好地处理用户定义的文字:
constexpr MonthDay operator "" _md ( unsigned long long arg )
{
return MonthDay (arg);
}
现在你可以在初始化失败时得到一个很好的错误:
MonthDay a = 15_md; // ok
MonthDay b = 32_md; // run-time error
如果在编译时使用超出范围的值进行初始化,这也会导致编译时错误,因为 assert
在构造函数的 if
子句中被计算:
constexpr MonthDay c = 15_md; // ok
constexpr MonthDay d = 32_md; // compile time error
这是一个demo
当我们定义一个太大而无法解析的内置类型变量时,Compiler/IDE(我使用的是Visual Studio 2019)会立即警告超出范围,甚至在之前编译项目 - 如果我们定义整数,则“整数常量太大”。
int sum = 100000000000000000000; // E0023, "integer constant is too large"
是否有可能在 c++/VS 中实现对用户定义类型的类似类型检查(我不确定这是否纯粹是 IDE 约束,可能是 - 我是 c++ 的新手)? 在某些情况下会有助于说明超出范围,例如
MonthDay day = 32; // error, "MonthDay literal can't be greater than 31."
Weight applesWeight = 2900000_kg; // "Item too heavy." Yet better if it could also work with custom literals.
来自 constexpr constructor with compile time validation 的版本使用 constexpr
,但这需要变量声明中的关键字。
class A
{
public:
constexpr A(int i) : i(i != 42 ? throw 42 : i) {}
private:
int i;
};
// usage
constexpr A ab = 43; // error, "Expression must have a constant value"
constexpr A abc = 42; // ok
=== 结果 ===
如Lev.M。在评论中提到这种类型的快速 'pre-compile-time' 分析是通过插件完成的(有时称为 linter,并且它们比其他静态分析器做更多的基本检查 - link 下面)并且显然 VC++ 2019 内置了一个新的 linter。这就是在键入代码时检查本机类型溢出的地方,而且此时似乎没有办法为 VC++ 扩展它(对于 C# 已经有一段时间了,如果你熟悉的话) Visual Studio、Roslyn 等)我想知道新的 VC++ linter 是否可以以某种方式扩展,但目前看来还没有。 将@cigien 的 post 标记为答案。
https://devblogs.microsoft.com/cppblog/intellisense-code-linter-for-cpp/
您可以在构造函数中添加检查参数是否在范围内,然后断言:
struct MonthDay
{
constexpr MonthDay (int data) : data(data)
{
if (data > 31)
{
assert(false); // months can't have more than 31 days
}
}
private:
int data;
};
只要构造函数是 constexpr
,它就可以很好地处理用户定义的文字:
constexpr MonthDay operator "" _md ( unsigned long long arg )
{
return MonthDay (arg);
}
现在你可以在初始化失败时得到一个很好的错误:
MonthDay a = 15_md; // ok
MonthDay b = 32_md; // run-time error
如果在编译时使用超出范围的值进行初始化,这也会导致编译时错误,因为 assert
在构造函数的 if
子句中被计算:
constexpr MonthDay c = 15_md; // ok
constexpr MonthDay d = 32_md; // compile time error
这是一个demo