I wrote this C++11 trait template to check whether a type is complete:
template <typename...>
using void_t = void;
template <typename T, typename = void>
struct is_complete : std::false_type
{};
template <typename T>
struct is_complete<T, void_t<decltype(sizeof(T))>> : std::true_type
{};
and tested it like this:
struct Complete {};
int main()
{
std::cout << is_complete<Complete>::value
<< is_complete<class Incomplete>::value
<< '
';
}
I expected the test program to print 10
, and that is the output I get when I compile it with clang 3.4. However, when compiled with gcc 4.9, it prints 11
instead -- mistakenly identifying class Incomplete
as complete.
I don't know for sure if my code is correct, but it seems to me that even if it's wrong, it should behave the same on both compilers.
Question 1: Is my code correct?
Question 2: Have I found a bug in one of the compilers?
EDIT:
I'm not asking for a replacement for my code. I'm asking whether there is a bug in gcc or clang, and whether or not this particular construct is correct.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…