Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the sizeof(T) safely in boost if T can be void?

I'm trying to figure our how I can get some code to compile that will determine the size of T's return value, where T is a function prototype, in my function template.

template<typename T>
void functionReturnLength()
{
long lReturnTypeSize = boost::mpl::eval_if<
    boost::is_void<boost::function_types::result_type<T>::type>::value, 
    boost::mpl::long_<0>,
    boost::mpl::long_<boost::mpl::sizeof_<boost::function_types::result_type<T>::type>::value>>::value;
}

However it still does not compile because sizeof(void) is not a valid operation - even though I am trying to construct an if-statement that will return a size of 0 if the type is void. I'm fairly new to BOOST MPL, so, while I have been browsing the documentation for some time, I am not sure how I could apply other ifs like if_ or apply_if, and if these would even work.

Thanks.

like image 273
Jeremy Avatar asked Dec 21 '22 05:12

Jeremy


1 Answers

You can use your own metafunction

template<typename T>
struct get_size { static const size_t value = sizeof(T); };

template<>
struct get_size<void> { static const size_t value = 0; };
like image 84
Abyx Avatar answered Dec 24 '22 02:12

Abyx