How can I get sizeof (T) safely in boost if T can be invalid?

I am trying to understand how I can get code to compile that will determine the size of the return value of T, where T is the prototype of the function 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 if I try to build an if statement that will return size 0 if the type is invalid. I am new to BOOST MPL, so while I have been looking through the documentation for some time, I am not sure how I could apply other ifs like if_ or apply_if, and even if they worked.

Thanks.

+4
source share
1 answer

You can use your own metaphor.

 template<typename T> struct get_size { static const size_t value = sizeof(T); }; template<> struct get_size<void> { static const size_t value = 0; }; 
+11
source

Source: https://habr.com/ru/post/1384303/


All Articles