|
Boost : |
Subject: [boost] [type_traits] is_complete<T> possible realization
From: Antony Polukhin (antoshkka_at_[hidden])
Date: 2012-11-05 03:34:47
Hi,
Yesterday I had a need to detect, is class forward declared or is it complete.
Here are some thoughts:
template <class T, int Misc>
struct is_complete {
typedef char false_type;
typedef int true_type;
template <class T1>
static typename boost::enable_if_c<sizeof(T1), true_type>::type test(int);
template <class T1>
static false_type test(...);
enum ENU { value = (
sizeof(test<T>(0)) == sizeof(true_type)
)};
};
'Misc' parameter is required to work around compilers one time instantiation:
struct incomplete;
template <int N> struct incomplete_template;
struct complete{};
struct complete_abstract{
virtual void foo() =0;
};
void test();
void foo1() {
cout
<< is_complete<incomplete, __LINE__>::value
<< is_complete<incomplete_template<0> , __LINE__>::value
<< is_complete<incomplete_template<1> , __LINE__>::value
<< is_complete<complete, __LINE__>::value
<< is_complete<complete_abstract, __LINE__>::value
<< endl;
}
struct incomplete{};
template <> struct incomplete_template<0>{};
void foo2() {
cout
<< is_complete<incomplete, __LINE__>::value // Without
second template parameter it would be still returning 0
<< is_complete<incomplete_template<0> , __LINE__>::value
<< is_complete<incomplete_template<1> , __LINE__>::value
<< is_complete<complete, __LINE__>::value
<< is_complete<complete_abstract, __LINE__>::value
<< endl;
}
int main()
{
foo1(); // Outputs 00011
foo2(); // Outputs 11011
return 0;
}
I was thinking of changing 'Misc' parameter definition to something like this:
template <class T, class... Misc>
struct is_complete ;
Than it can be used in template functions/classes just like this:
template <class T, class... SomeClasses>
void test_function() {
is_complete<T, SomeClasses..., boost::mpl::int_<__LINE__> >::value;
}
So,
Is there interest in such metafunction?
Can somebody give advices how to improve it? (I`m not sure that
implementation is portable, I`ve tested it only on GCC-4.6 and
CLANG-3.0, MSVC compiler will be available only tomorrow)
-- Best regards, Antony Polukhin
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk