問題描述
在回答我該怎么做編寫一個看起來像方法的 lambda 表達式?,我試圖通過利用以下事實將無捕獲的 lambda 轉換為成員函數指針,因為自 C++17 起,無捕獲的 lambda 具有 constexpr 將運算符轉換為其函數指針類型.
While answering How do I write a lambda expression that looks like a method?, I tried to turn a captureless lambda into a member function pointer by exploiting the fact that, since C++17, captureless lambdas have a constexpr conversion operator to their function pointer type.
所以我想出了一個問題:
So I came up with an issue boiling down to:
template<void(*)()> struct A{};
int main()
{
A<static_cast<void(*)()>([]{})>{}; // 1
constexpr auto fp = static_cast<void(*)()>([]{});
A<fp>{}; // 2
}
現在,這在 clang(自 5.0.0 起)中編譯,但 gcc(>=7.2) 抱怨:
Now, this compiles in clang (since 5.0.0) but gcc(>=7.2) complains:
error: lambda-expression in template-argument
A<static_cast<void(*)()>([]{ /*whatever*/ })>{}; // 1
^
error: 'main()::<lambda()>::_FUN' is not a valid template argument for type 'void (*)()' because 'static constexpr void main()::<lambda()>::_FUN()' has no linkage
A<fp>{}; // 2
問題是,誰是對的?
推薦答案
這是一個 gcc 錯誤,已提交 83258.
This is a gcc bug, filed 83258.
在 C++14 中,我們曾經有一個 鏈接要求指針類型的非類型模板參數.但是在 C++17 中(由于 N4268),參數只需要一個轉換正確類型的常量表達式,還有一些其他限制(此處均不相關).一旦我們可以構造fp
,我們就應該能夠將其用作模板參數.
In C++14, we used to have a linkage requirement for non-type template parameters of pointer type. But in C++17 (as a result of N4268), the parameter just needs to be a converted constant expression of the correct type, with a few other restrictions (none of which are relevant here). Once we can construct fp
, we should be able to use it as a template parameter.
這篇關于我可以將 C++17 無捕獲 lambda constexpr 轉換運算符的結果用作函數指針模板非類型參數嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!