C++20 是否允许通过非类型模板参数中的 class 类型传递函数指针?
Is passing of a function pointer through a class type in non-type template parameter allowed in C++20?
最近在玩弄C++20的可以通过class types in non-type template parameters (P0732R2)的特性后,遇到了一些比较奇怪的事情。
考虑以下代码:
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 answer 说函数指针应该是符合 C++ 标准的有效模板参数。
这里哪个编译器是正确的?
此外,奇怪的是,相同的代码 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. 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?
Which compiler is correct here?
Clang 是正确的,所写的代码是有效的。
GCC 不处理该特定案例是已知的 issue。
最近在玩弄C++20的可以通过class types in non-type template parameters (P0732R2)的特性后,遇到了一些比较奇怪的事情。
考虑以下代码:
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 answer 说函数指针应该是符合 C++ 标准的有效模板参数。
这里哪个编译器是正确的?
此外,奇怪的是,相同的代码 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. 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?
Which compiler is correct here?
Clang 是正确的,所写的代码是有效的。
GCC 不处理该特定案例是已知的 issue。