Boost logo

Boost :

Subject: Re: [boost] boost::optional<void>
From: Daniel Larimer (dlarimer_at_[hidden])
Date: 2010-03-01 09:21:09


On Mar 1, 2010, at 5:24 AM, Thorsten Ottosen wrote:

> Daniel Larimer skrev:
>> I am writing some generic code and would like to handle "all values" the same way; however, I keep running into the problem of return void return types that need "special treatment". There is nothing I can do about it most of the time except template specialization. That said, it would be helpful if boost::optional<void> could have some sensible default implementation where it acts more or less like a boolean. As it is, I will probably provide my own specialization of boost::optional to simplify my templates.
>> Any thoughts on this?
>
> Could you give some concrete examples of your problems? Otherwise it's hard to comment.
>
        template<typename ReturnType, TypeName A1, ... >
        boost::optional<ReturnType> some_templated_method( A1 a1, ... )
        {
                if( .... )
                        return a1 + ...
                else
                        return boost::optional<ReturnType>()
        }
        
        The above template works for all functions up to N parameters but not for void functions. In my situation I have an active objects implementation where a "void" return value signifies "operation completed" so where as sequential code can take "operation complete" for granted, async code cannot. In my particular case, I was using a boost::optional to store the "future" value of the async operation, but had to duplicate and "tweak" several classes for the "void" case.
        
        The question is, does issuing a compile error on "sizeof(void)" make more sense than having an optional<void> which will work "properly" for everything except "cast to reference" and "cast to value"? Essentially, let there be compile errors about those methods missing if someone tries to use them, but other methods that return a pointer to T can return a NULL void* without issue.

        If you think about how optional<T> is implemented, a bool followed by sizeof(T) bytes, then if follows that a optional<void> would be a bool followed by 0 bytes.

> -Thorsten
> _______________________________________________
> Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk