Re: detecting, at compile-time, whether a type is complete
Thats an interesting problem. Alot depends on how "incomplete type is
defined".
I like the definition that it is types of unknow size. Then if the
sizeof operator fails on incomplete types, it opens up the possibility
of detecting incomplete types with a metaprogram using SFINAE.
I tried making a such a metaprogram but failed. I could not get a
overloaded function parameter to depend on the success or failiure of
the sizeof operator (for a given type T).
Im think my test program fails because it is not strictly SFINAE.. As
in the overload failure dosn't happen when substituting the parameter
type, but when instantiating the parameter type to be subsituted..
I heard some people talking about IFINAE ("Instantiation failure is
not an error") as a complement to SFINAE. Sound like the same problem.
Anyway here is the code. If anyone knows of how to fix it or make
another one that separates the types that fail the sizeof(T) operator
from the ones that succeed then please tell. (I know such a traits
class could be made manually by partial specializations, but thats a
bit like cheating ;) )
<CODE>
typedef char My_No;
typedef struct{char a[2];} My_Yes;
template <typename T>
struct SizeOf {
enum {r = sizeof(T)};
};
template <typename T, int>
struct IntValueToType {
typedef int Type;
};
template <typename T>
My_Yes IsCompleteFunc(typename IntValueToType<T, SizeOf<T>::r>::Type =
0);
template <typename T>
My_No IsCompleteFunc(...);
template <typename T>
struct isComplete {
enum {r = sizeof(IsCompleteFunc<T>()) == sizeof(My_Yes)};
};
// Just some incomplete some complete test classes..
struct NoBodyClass;
class ABIClass {
virtual void somefunc() = 0;
virtual ~ABIClass();
};
struct EmptyClass {};
struct RegularClass {
RegularClass();
~RegularClass();
int i;
};
#include <iostream>
inline void Test() {
//incomplete types: commented out since they wont compile..
//std::cout << "isComplete<void>::r " << isComplete<void>::r
<<std::endl;
//std::cout << "isComplete<NoBodyClass>::r " <<
isComplete<NoBodyClass>::r <<std::endl;
////std::cout << "isComplete<int[]>::r " << isComplete<int[]>::r
<<std::endl;
//complete types:
std::cout << "isComplete<EmptyClass>::r " <<
isComplete<EmptyClass>::r <<std::endl;
std::cout << "isComplete<RegularClass>::r " <<
isComplete<RegularClass>::r <<std::endl;
std::cout << "isComplete<ABIClass>::r " << isComplete<ABIClass>::r
<<std::endl;
std::cout << "isComplete<int>::r " << isComplete<int>::r <<std::endl;
std::cout << "isComplete<void*>::r " << isComplete<void*>::r
<<std::endl;
}
</CODE>
Regards Patrik
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]