[英]Is passing of a function pointer through a class type in non-type template parameter allowed in C++20?
Recently, after playing around with the C++20 feature of being able to pass class types in non-type template parameters ( P0732R2 ) , I've encountered something rather strange.最近,在尝试了能够在非类型模板参数 ( P0732R2 ) 中传递类类型的 C++20 特性之后,我遇到了一些很奇怪的事情。
Consider the following code:考虑以下代码:
template <typename T>
struct abc {
T p;
consteval abc(T p) : p(p) { }
consteval operator decltype(p)() const {
return p;
}
};
template <abc obj>
consteval auto do_something1() {
return obj();
}
template <auto obj>
consteval auto do_something2() {
return obj();
}
constexpr auto fun() {
return true;
}
int main() {
static_assert(do_something1<&fun>()); // OK in Clang, fails in GCC
static_assert(do_something2<&fun>()); // OK in both GCC and Clang
}
This appears to work as intended with clang however GCC fails to compile this and gives an error saying that fun
is not a valid template argument .这似乎与 clang 预期的一样,但是 GCC 无法编译它并给出错误,指出fun
不是有效的模板参数。
However, this answer says that function pointers should be valid template arguments as per the C++ standard.但是,这个答案说,根据 C++ 标准,函数指针应该是有效的模板参数。
Which compiler is correct here?哪个编译器在这里是正确的?
Also, weirdly enough, the same code appears to work on both compilers when modified to work with member functions for some reason .此外,奇怪的是,当出于某种原因修改为与成员函数一起使用时,相同的代码似乎可以在两个编译器上运行。
Note : Something that might be useful to note is that according to this page, GCC's support for P0732R2 is complete while Clang's is still in a partial/experimental stage.注意:需要注意的一点是,根据此页面,GCC 对P0732R2的支持已完成,而 Clang 仍处于部分/实验阶段。 So, does this mean that GCC is correct and passing of function pointers through class types in non-type template parameters shouldn't be possible, or am I missing something here?那么,这是否意味着 GCC 是正确的,并且通过非类型模板参数中的类类型传递函数指针是不可能的,还是我在这里遗漏了什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.