c++ - Getting SFINAE correct with functions -


I have written a quick thing that determines that certain types (and arguments) are variable for plain function pointer Whether or not it works:

  template & lt; Typename F, typename ... args & gt; Struct is_stateless: std :: is_convertible & lt; F, typename std :: result_of & lt; F (Args ...) & gt; :: Type (*) (Args ...) & gt; :: type {};   

It works for the longest time, but it has a serious drawback: std :: result_of & lt; F (Args ...) & gt; :: . If you accidentally gave incorrect arguments for your function (say that you have left & amp; ) due to result_of you type Field I like my is_stateless structure to be "false" in this case (as you like a "right" fun and logic that is not meant to be variable in a function Indicator).

I am open to using the features present in C ++ 11 and C + 1 GCE 4.8. * And below. Any ideas on how to do it?

institutionalizing result_of < F (Args ...) & Gt; Where is not right for F for args is not allowed if you do it anyway, there is no way to recover from the error.

What you can do to avoid using result_of : You can change the same code as decltype (declval & lt; f & gt; () (declval & lt; Args & gt; () ...)) . This should also work with SFINAE references:

  #include & lt; Utility & gt; # Include & lt; Vector & gt; Template & lt; Typename F, typename ... Args & gt; Zero funcptr_helper (...); Template & lt; Typename F, typename ... Args & gt; Auto Fconpt_Helper (F, ARG ...) - & gt; Decltype (std :: declval & lt; f & gt; () (std :: declval & lt; args & gt; () ...)); Template & lt; Typename F, typename ... Args & gt; Struct funcptr {typedef decltype (funcptr_helper  (std :: declval & lt; F & gt; (), std :: declval & lt; arrags & gt; () ... )) (* Type) (args ...); }; Template & lt; Typename F, typename ... Args & gt; Struct is_stateless: std :: is_convertible & lt; F, typewrite phonictptor & lt; F, Args ... & gt; :: type & gt; :: type {}; Int main () {auto f1 = [] (int i) {return i; }; Static_assert (is_stateless & lt; decltype (f1), int & gt; :: value, ""); Int j = 0; Auto f2 = [=] (int i) {return i + j; }; Static_assert (! Is_stateless & lt; decltype (f2); int & gt; :: value, ""); Static_assert (is_stateless & lt; std :: vector & lt; int & gt; & amp; (*) (std :: vector & lt; int & gt; int, char, bool), std :: vector & lt; Int & gt; and, int, char, bool & gt; :: value, ""); Static_assert (! Is_stateless & lt; std :: vector & lt; int & gt; & amp; (*) (std :: vector & lt; int & gt; int, char, bool) ""); Static_assert (! Is_stateless & lt; std :: vector & lt; int & gt; and (*) (std :: vector & lt; int & gt; int, char, bool), const std :: vector & lt; Int & gt; int, char, bool & gt; :: value, ""); }    

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -