你明白为什么static_assert失败了:
template <typename T>
void
foo(const T &c)
{
static_assert(std::is_base_of<T, char>::value, "T must be char"); // Fails !
}
int main()
{
char c = 'a';
foo<char>(c);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我交换了T和"char",仍然失败了.
您可能需要考虑添加其他检查:
template <typename T> void foo(const T &c)
{
static_assert(
std::is_base_of<T, char>::value || std::is_same<T, char>::value,
"T must be char");
}
Run Code Online (Sandbox Code Playgroud)
但如果你只关心字符,那么你可以做:
static_assert(std::is_same<T, char>::value, "T must be char");
Run Code Online (Sandbox Code Playgroud)
还要考虑is_fundamental,以及is_convertible更复杂的断言.
| 归档时间: |
|
| 查看次数: |
133 次 |
| 最近记录: |