![](/img/trans.png)
[英]Is placeholder for the deduced class type of non-type template parameter a C++20 feature?
[英]Is passing of a function pointer through a class type in non-type template parameter allowed in C++20?
最近,在嘗試了能夠在非類型模板參數 ( P0732R2 ) 中傳遞類類型的 C++20 特性之后,我遇到了一些很奇怪的事情。
考慮以下代碼:
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
}
這似乎與 clang 預期的一樣,但是 GCC 無法編譯它並給出錯誤,指出fun
不是有效的模板參數。
但是,這個答案說,根據 C++ 標准,函數指針應該是有效的模板參數。
哪個編譯器在這里是正確的?
此外,奇怪的是,當出於某種原因修改為與成員函數一起使用時,相同的代碼似乎可以在兩個編譯器上運行。
注意:需要注意的一點是,根據此頁面,GCC 對P0732R2的支持已完成,而 Clang 仍處於部分/實驗階段。 那么,這是否意味着 GCC 是正確的,並且通過非類型模板參數中的類類型傳遞函數指針是不可能的,還是我在這里遺漏了什么?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.