【发布时间】:2021-01-23 20:47:03
【问题描述】:
如何编写一个检测算术类型指针的概念?
template <typename T>
concept arithmetic = std::is_arithmetic<T>::value;
template <typename T>
concept pointer_to_arithmetic = requires (T a) {
{ *a } -> arithmetic;
};
template <typename T>
void fn() {
printf("fail\n");
}
template <pointer_to_arithmetic T>
void fn() {
printf("pass\n");
}
struct s{};
int main() {
fn<int>();
fn<int*>();
fn<s>();
fn<s*>();
}
我尝试了上面的方法,它可以编译,但没有做它应该做的事情。
预期输出是:
fail
pass
fail
fail
相反,我得到:
fail
fail
fail
fail
如果我将*a 替换为a[0],它也不起作用。
【问题讨论】:
标签: c++ c++20 typetraits c++-concepts