static assert that template typename T is NOT complete?
c++, incomplete-type, static-assert, templates
Solution
Here is a function using expression SFINAE based on chris proposal which allows checking whether a type is complete yet. My adoption needs no includes, errors-out when the required argument is missing (hiding the argument was not possible) and is suitable for C++11 onward.
template<typename T>
constexpr auto is_complete(int=0) -> decltype(!sizeof(T)) {
return true;
}
template<typename T>
constexpr bool is_complete(...) {return false;}
And a test-suite:
struct S;
bool xyz() {return is_complete<S>(0);}
struct S{};
#include <iostream>
int main() {
std::cout << is_complete<int>(0) << '\n';
std::cout << xyz() << '\n';
std::cout << is_complete<S>(0);
}
Output:
1
0
1
See live on coliru
Problem
Is there a way to static_assert that a type T is Not complete at that point in a header? The idea is to have a compile error if someone adds #includes down the road in places they should not be. related: How to write `is_complete` template? Using that link's answer, ``` namespace { template<class T, int discriminator> struct is_complete { static T & getT(); static char (& pass(T))[2]; static char pass(...); static const bool value = sizeof(pass(getT()))==2; }; } #define IS_COMPLETE(X) is_complete<X,__COUNTER__>::value class GType; static_assert(!IS_COMPLETE(GType),"no cheating!"); ``` unfortunately this gives "invalid use of incomlete type" error, d'oh. Is there a way to assert on the negation?