Considering the scope of these two libraries, I still wonder which one to use in some cases. For example, let's say I have a set of functions that can only take integers as parameters, what's the correct practice : use enable_if to have SFINAE makes the compiler outputs something like "no such fonction" or have a concept check for Integer and have a concept related error indicating people what happens. Is there any policies or is this a matter of tastes ?
AMDG joel falcou wrote:
Considering the scope of these two libraries, I still wonder which one to use in some cases. For example, let's say I have a set of functions that can only take integers as parameters, what's the correct practice : use enable_if to have SFINAE makes the compiler outputs something like "no such fonction" or have a concept check for Integer and have a concept related error indicating people what happens.
Is there any policies or is this a matter of tastes ?
If you might want another function to be selected instead, use enable_if If you just want an error message use concept checks. In Christ, Steven Watanabe
Another question about concepts : how can I build a concept which requires a given type X to either have an associated type named foo or an associated type named bar (silly names for example purpose). Checking for both is easy but i can't find how to check for OR another. Thanks in advance
Hello,
how can I build a concept which requires a given type X to either have an associated type named foo or an associated type named bar (silly names for example purpose). Checking for both is easy but i can't find how to check for OR another.
Maybe using SFINAE? template <class T> struct foo_or_bar { template <class U> static typename U::bar f(); template <class U> static typename U::foo f(); typedef BOOST_TYPEOF(f<T>()) type; }; foo_or_bar<something>::type will give you the type of the "foo" or "bar" nested in "something"... But it's an exclusive OR: if both are present, you have an ambiguity. Anyway I'm pretty sure someone else will find a better way... :-) Bruno
on Sun Aug 10 2008, "joel falcou"
Another question about concepts :
how can I build a concept which requires a given type X to either have an associated type named foo or an associated type named bar (silly names for example purpose). Checking for both is easy but i can't find how to check for OR another.
I would say don't. It's not friendly to generic code and it sounds like "or constraints" which were left out of C++0x for very good reasons. If you could be more specific about what you're trying to do I might be able to help find a better solution. My first guess is that the answer will be found in the use of traits. -- Dave Abrahams BoostPro Computing http://www.boostpro.com
I would say don't. It's not friendly to generic code and it sounds
"or constraints" which were left out of C++0x for very good reasons. If you could be more specific about what you're trying to do I might be able to help find a better solution. My first guess is that the answer will be found in
like the use of traits. Basically I want to make a concept which represents class that supports the result_of protocol, ie exposes either a result_type or a result inner class. So my first idea was to see if a given type have either of this inner elements.
On Sun, Aug 10, 2008 at 11:57 PM, joel falcou
Basically I want to make a concept which represents class that supports the result_of protocol, ie exposes either a result_type or a result inner class. So my first idea was to see if a given type have either of this inner elements.
I tried to make something I called result_of_defined a while back, which I think tried to accomplish something like what you're mentioning: http://svn.boost.org/svn/boost/sandbox/SOC/2007/signals/boost/dataflow/signa... The test for that was: http://svn.boost.org/svn/boost/sandbox/SOC/2007/signals/libs/dataflow/test/s... At some point I gave up, because I never really got to understand all the details of result_of, and I started to suspect that what I wanted to do was not fully possible. I haven't been running the above test for a while so I don't remember what all was passing at the time I left it, but maybe the result_of_defined code will be of some use to you as a reference. Regards, Stjepan
Thansk for the clues. I indeed turned to a traits based solution. I must admit that, if testing for the result_type typedef is easy, testing if a template inner class with an a priori unknown number of template parameters is harder. It's even harder cause I can't use a simple SFINAE on the result of boost::result_of cause it's defined even if neither result_of protocols are implemented. I'll try some exhaustive listing of result possibilities and see what it spawns.
Forgot what I just said. I got this working : http://codepad.org/mcY4K7Sf Do anyone see any blatant misses in this ?
on Mon Aug 11 2008, "joel falcou"
Forgot what I just said. I got this working :
Do anyone see any blatant misses in this ?
Doesn't work for
struct me2
{
template
Doesn't work for : <snip>
Yes, I thought that the boost::result_of protocol needed the result struct to be a one argument template only. I forgot about default argument and such. Well, I used some preprocessor magic and got this : http://codepad.org/VrLTKmJO Should do the tric IMHO. Comments on how to simplify this is of course welcomed. If it's of some interest, I can make a proper file to be included in the proper boost library (maybe next to result_of ?)
On Mon, Aug 11, 2008 at 9:05 AM, joel falcou
Yes, I thought that the boost::result_of protocol needed the result struct to be a one argument template only. I forgot about default argument and such.
Well, I used some preprocessor magic and got this : http://codepad.org/VrLTKmJO
Should do the tric IMHO. Comments on how to simplify this is of course welcomed. If it's of some interest, I can make a proper file to be included in the proper boost library (maybe next to result_of ?)
What is your use case? I found this sort of thing useful for finding out whether result_of would work for a specific set of arguments. Is your metafunction intended for testing whether result_of would work for _some_ (whichever) set of arguments? (it doesn't quite do that because it doesn't check that result has a nested type typedef). Stjepan
What is your use case? I found
Stjepan Rajko a écrit : this sort of thing useful for finding
out whether result_of would work for a specific set of arguments. Is your metafunction intended for testing whether result_of would work for _some_
(whichever) set of arguments? (it doesn't quite do that
because it doesn't check that result has a nested type typedef).
I was
trying to use it as a simple traits to see if a given type fullfilled the
result_of protocol
whatever the arguments used. It works ok for the
result_type variant but now I see it's still incomplete. The problem is
that I can't see, for an arbitrary result structure, how I can see if it
actually has at least one overload that has a proper nested type
typedef.
Typical use is :
For F a given type,
support_result_of_protocol<F>::type evaluates as a compile time
boolean indicating if either F has a nested result structure or
result_type typedef. It doesn't make any assumptions on wether or nor
result is correcly implemented.
As for testing if a particuliar
set of arguments produces a valid result_of, one can easily do :
template< class F
,
template<class> class T = boost::result_of<F>::type
> struct valid_impl
{
typedef void type;
};
template
On Mon, Aug 11, 2008 at 9:33 AM, joel falcou
I was trying to use it as a simple traits to see if a given type fullfilled the result_of protocol whatever the arguments used. It works ok for the result_type variant but now I see it's still incomplete. The problem is that I can't see, for an arbitrary result structure, how I can see if it actually has at least one overload that has a proper nested type typedef.
I can't either :-)
Typical use is :
For F a given type, support_result_of_protocol<F>::type evaluates as a compile time boolean indicating if either F has a nested result structure or result_type typedef. It doesn't make any assumptions on wether or nor result is correcly implemented.
As for testing if a particuliar set of arguments produces a valid result_of, one can easily do :
template< class F , template<class> class T = boost::result_of<F>::type > struct valid_impl { typedef void type; };
template
struct produce_valid_result_of : boost::false_type {}; template<class F> struct produce_valid_result_of
: boost::true_type {}; or did I miss something in your question ?
I think you got my question, but that won't work. If you fix it to:
template< class F
, typename T = typename boost::result_of<F>::type
> struct valid_impl
{
typedef void type;
};
template
With the way boost::result_of is implemented, I couldn't find a way to make an approach like the above work. The best I could do was the result_of_defined code.
So we're stuck with the implementation I gave earlier but with a rather weak semantic as some type can give false positive ... For me it's enough anyway as most of the time the problem lies in a missing result struct. So anyway, what can we do to access this ?
participants (5)
-
Bruno Lalande
-
David Abrahams
-
joel falcou
-
Steven Watanabe
-
Stjepan Rajko