简体   繁体   English

在 C++20 中是否允许通过非类型模板参数中的类类型传递函数指针?

[英]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 是正确的,并且通过非类型模板参数中的类类型传递函数指针是不可能的,还是我在这里遗漏了什么?

Which compiler is correct here?哪个编译器在这里是正确的?

Clang is correct, the code as written is valid. Clang 是正确的,所写的代码是有效的。

GCC not handling that specific case is a known issue . GCC 不处理该特定情况是一个已知问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 非类型模板参数的推导 class 类型的占位符是 C++20 功能吗? - Is placeholder for the deduced class type of non-type template parameter a C++20 feature? c++ 20中如何静态断言该类型对于模板非类型参数是可行的 - How to static_assert that type is viable for template non-type parameter in c++20 C++20:非类型模板参数中的非捕获 lambda - C++20: Non-capturing lambda in non-type template parameter 将函数指针作为非类型模板参数传递 - Passing a function pointer as non-type template parameter C++20 非类型模板参数,即先前类型参数中的模板:不是有效的模板参数,因为不是变量 - C++20 non-type template parameter which is template in the prior type parameters: is not a valid template arg, because is not a variable 函数指针作为非类型模板参数 - Function pointer as non-type template parameter C ++不允许使用“ this”作为非类型模板参数 - C++ Using 'this' as non-type template parameter not allowed C++20 标准对使用子对象作为模板非类型 arguments 有什么看法? - What does the C++20 standard say about usage of subojects as template non-type arguments? 函数指针与作为模板非类型参数的指针 - Pointer to function vs function as template non-type parameter "通配符函数指针非类型模板参数" - wildcard function pointer non-type template parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM