I have test code as below.
#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;
}
Compile with g , compiler will complain:
test.cpp:12:27: error: explicit template argument list not allowed
12 | static constexpr bool check<T, std::void_t<typename T::iterator>> = true;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
But the clang compiles the code without any error.
What does the error g thorws mean? How can I modify the code that both g and clang compiles it?
Thanks!
CodePudding user response:
It should be ok to write a partial specialization in any context
It is listed as a gcc bug but it is not fixed yet: gcc bug
As a workaround you can place the specialization outside class context like:
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;
}