Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a compiler bug exposed by my implementation of an is_complete type trait?

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.

like image 720
Mircea Ispas Avatar asked Sep 14 '14 12:09

Mircea Ispas


1 Answers

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.

like image 76
T.C. Avatar answered Sep 20 '22 06:09

T.C.