26

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
              << '\n';
}

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.

T.C.
  • 133,968
  • 17
  • 288
  • 421
Mircea Ispas
  • 20,260
  • 32
  • 123
  • 211
  • Related: http://stackoverflow.com/questions/25796126/static-assert-that-template-typename-t-is-not-complete – chris Sep 14 '14 at 12:45
  • @chris - this is not a duplicate - the question is about the possible bug from gcc or clang. – Mircea Ispas Sep 14 '14 at 12:48
  • I didn't say it was a dupe, or vote to close as such. If you want something that works on both GCC and Clang, it's helpful. – chris Sep 14 '14 at 12:52
  • @Felics that was me, and I see now what you mean. Please strive to make your question title as descriptive as possible. Only the last sentences of your question body really talk about a compiler bug. It is far from clear that this is the core matter you're asking a question about. I have modified your question title to this effect. Sorry for the premature closing. – rubenvb Sep 14 '14 at 13:03
  • The problematic bit with an `is_complete` type trait is that its value is determined by the context in which is first instantiated. You can't get different `is_complete::value` in different places. – K-ballo Sep 14 '14 at 20:25
  • @K-ballo Yes, I know. I'm using it as a static assert to check if a type has a member function. You can find more about how I use it here: http://pushcpp.blogspot.com/2014/09/modern-c-at-work-to-build-extensible.html – Mircea Ispas Sep 15 '14 at 07:30

1 Answers1

28

The problem appears to be with the definition of void_t. Defining it as

template<typename... Ts>
struct make_void { typedef void type;};

template<typename... Ts>
using void_t = typename make_void<Ts...>::type;

instead yields the correct result (10) on both compilers (Demo).

I believe this is the same issue noted in section 2.3 of N3911, the paper proposing void_t, and CWG issue 1558. Essentially, the standard was unclear whether unused arguments in alias template specializations could result in substitution failure or are simply ignored. The resolution of the CWG issue, adopted at the Committee's November 2014 meeting, clarifies that the shorter definition of void_t in the question should work, and GCC 5.0 implements the resolution.

T.C.
  • 133,968
  • 17
  • 288
  • 421