如何static_assert解析为 const 的值是正确的类型?
How do I static_assert that a value parsed as const& is the right type?
MyClass::MyClass(std::list<int> const& some_sequence)
{
static_assert(
std::is_same<decltype(some_sequence),std::list<int>>::value ,
"some_sequence should be an integer list"
);
}
如何使静态断言工作?重要的是类型是一个整数列表。干杯。
不需要使用
static_assert(...)
:编译器将确保使用std::list<int>
调用此函数。如果要使上面的代码编译,则需要使用
MyClass::MyClass(std::list<int> const& some_sequence)
{
static_assert(
std::is_same<decltype(some_sequence),std::list<int> const&>::value ,
"some_sequence should be an integer list"
);
}
some_sequence
被声明为std::list<int> const&
,这就是由 decltype(some_sequence)
获得的类型。然而,这种static_assert()
永远不会失败。
相关文章:
- 递归模板化函数不能分配给具有常量限定类型"const tt &"的变量"state"
- 视觉工作室 2017;启用 /permissive 时,类型 "const wchar_t *" 的参数与类型 "PWSTR" 的参数不兼容
- 无法使用类型 'const char *' 的左值初始化类型 'char *' 的成员子对象
- "const wchar_t *" 类型的值不能用于初始化类型 "const PWSTR" 的实体
- 错误:对类型 'const ItemInstance' 的引用无法绑定到类型 'void' 的右值
- C++ 类型 "char" 的参数与类型 "const char" 的参数不兼容
- 在这种情况下,如何从类型 "const char*" 初始化字段?
- 类型"const char[2]"的值不能隐式转换为"int"错误C++
- 错误:数组下标的类型"const bool[int]"无效
- 错误:C2679 二进制"==":未找到采用类型 'const std::string' 的右侧操作数的运算符(或者没有可接受的转换)
- 类型 "const char *" 的参数与类型 "char *" 的参数不兼容
- 错误:"->"的基本操作数具有非指针类型"const"
- 作为返回类型"const int&"做什么?
- 类型 "const char*" 的参数与类型 "char*" 的参数不兼容。但是为什么?:(
- 类型 "const char*" 的参数与类型 "Person" 的参数不兼容
- C++:"错误:类型'const char*'和'const char [28]'的操作数无效到二进制'ope
- 类型 "const char *" 的参数与类型 "LPCWSTR" 的参数不兼容
- 错误:从不兼容的类型 'const char *' 分配给'char'
- 类型 "const char *" 的默认参数与类型 "char *" 的参数不兼容
- 类型 "const char *" 的参数与 "LPSTR" C++ 类型的参数不兼容