![](/img/trans.png)
[英]enable_if + is_same + constexpr function makes MSVC fail (but works well in Clang, GCC)
[英]enable_if works with gcc but not with clang and msvc
我了解了 SFINAE 原理及其各種用途。 然后我編寫了以下程序,該程序使用 gcc 進行編譯,但不使用 msvc 和 clang 進行編譯。現場演示。
#include <iostream>
#include <type_traits>
template <typename T> class Container {
public:
template<typename U = T>
std::enable_if_t<std::is_same_v<T, int>> foo(const T&)
{
}
};
template<typename T>
void func(T&& callable)
{
Container<int> c;
(c.*callable)(4);
}
int main(){
//works with gcc but not with clang and msvc
func(&Container<int>::foo);
}
正如我們所看到的,上面的程序適用於 gcc 但不適用於 clang 和 msvc,我不知道哪個編譯器就在這里。 那么這個程序是良構的還是病態的等等。
這是CWG 2608 ,程序格式正確。
如果所有模板 arguments 都可以從默認模板參數中推導出或獲得,則可以將它們全部省略; 在這種情況下,空模板參數列表 <> 本身也可以省略。
(強調我的)
請注意添加的部分func(&Container<int>::foo);
格式良好,因為U
的模板參數可以從默認模板參數獲得,因此它可以被省略,這意味着空模板參數列表<>
本身也可以被省略。 因此,gcc 符合標准。
這是 clang 錯誤:
這是 msvc 錯誤:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.