我有如下測驗代碼。
#include <iostream>
#include <vector>
using namespace std;
template<typename Cont>
class Test
{
template<typename T, typename = void> static constexpr bool check = false;
template<typename T>
static constexpr bool check<T, std::void_t<typename T::iterator>> = true;
public:
static bool fun()
{
return check<Cont>;
}
};
int main([[maybe_unused]] int argc, [[maybe_unused]] char *argv[])
{
cout << Test<vector<int>>::fun() << endl;
cout << Test<int>::fun() << endl;
return 0;
}
用 g 編譯,編譯器會報錯:
test.cpp:12:27: error: explicit template argument list not allowed
12 | static constexpr bool check<T, std::void_t<typename T::iterator>> = true;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
但是clang 編譯代碼沒有任何錯誤。
錯誤 g thorws 是什么意思?如何修改 g 和 clang 編譯的代碼?
謝謝!
uj5u.com熱心網友回復:
在任何情況下都可以撰寫部分專業化
它被列為 gcc 錯誤,但尚未修復:gcc bug
作為一種解決方法,您可以將專業化放在類背景關系之外,例如:
template<typename Cont>
class Test
{
template<typename T, typename = void> static constexpr bool check = false;
public:
static bool fun()
{
return check<Cont>;
}
};
template<typename Cont>
template<typename T>
constexpr bool Test<Cont>::check<T, std::void_t<typename T::iterator>> = true;
int main([[maybe_unused]] int argc, [[maybe_unused]] char *argv[])
{
cout << Test<vector<int>>::fun() << endl;
cout << Test<int>::fun() << endl;
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/473685.html
上一篇:使用模板在類中設定函式型別會出錯
