4

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.

1 Answer 1

11

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; };
22
  • 2
    Better yet, he could say value = void_has_no_size(), which will make the compiler complain that there's no such function as void_has_no_size().
    – Paul Manta
    Commented Dec 2, 2011 at 11:47
  • 6
    @Paul Manta: The OP's desire is to get 0 for void. Commented Dec 2, 2011 at 11:55
  • 1
    @curiousguy Obviously. But in some cases that's a desirable thing.
    – Paul Manta
    Commented Dec 2, 2011 at 14:22
  • 2
    @curiousguy When you know your template will not function correctly with a certain type, you make it so that you cannot compile that specific type.
    – Paul Manta
    Commented Dec 2, 2011 at 14:44
  • 1
    @curiousguy: Consider static_assert Commented Dec 2, 2011 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.