c++ - How to use `is_member_function_pointer` to include/exclude member function calls in algorithms? -


I am attempting to use within an algorithm, either to make a specific call to a specific member function Do not do anything (if it exists) or do anything:

  template & lt; Class T & gt; Struct is_member_function_pointer;   

Checks whether T is a non-static member function pointer. Member provides stable value which is equal to true, if T is a non-static member function pointer type. Otherwise, the value is equal to wrong.

For example:

  #include & lt; Iostream & gt; # Include & lt; Type_traits & gt; #include & lt; Vector & gt; Struct a {zero bow () console {std :: cout & lt; & Lt; "Boo." & Lt; & Lt; Std :: endl; }}; Structure B {}; The template's & lt; Bool & gt; Loose structure; The template's & lt; & Gt; Strip bioyabel & lt; True> {Template & lt; Typename type & gt; Static zero booIt (type const & amp; t) {t .boo (); }}; The template's & lt; & Gt; Strip bioyabel & lt; False & gt; {Template & lt; Typename type & gt; Static zero byte (typecast & amp; t) {std :: cout & lt; & Lt; "Non-bioyable type of burden" & lt; & Lt; Std :: endl; }}; Template & lt; Typename type & gt; Zero alg (type const & amp; t) {booable & lt; Std :: is_member_function_pointer & lt; Decltype (& type :: boo) & gt; :: value & gt; :: booIt (t); } Int main (int argc, const char * argv []) {a a; BB; Alg (a); Alg (b); Return 0; }   

The structure is a is booable and b is not. The algorithm is dependent on the alg , is_member_function_pointer inside the boolean value to false or true if it depends on the fact that The type implements a member function bu . This Boolean value is then used to specialize in the booable structure, which implements the booIt stable member function whose sole purpose call boo Booable object, or do nothing and notify the user

However, it is compiled (saved IM main.cpp ) , Result in the following compile time error:

  main.cpp: in a type of futuristic alge (const type & amp;) [instant = b] With: main.cpp: 46: 10: here is the main.cpp: 37: 54: error: a ?? ?? Booâ ???? Not a member of "BA" booable & LT; Std :: is_member_function_pointer & lt; Decltype (& type :: buoy) & gt; :: Price & gt; :: booIt (t);   

I wonder: is not a usable boolean value and a compile-time error to indicate this attribute? This is the same error that is just done by me

  B; B.boo ();   

What have I done here?

This is not reporting as usable boolean values ​​and this property structure issue Not a compile time error?

Yes, but to test a member of a member, that member needs survival.

You get an error because b does not have boo and result in alg decltype (and b :: BOO) . You will not expect to compile decltype (& int :: foo) , do you want

what you want

  • For example

  • A specialist template that gives you further examination to use the results of the investigation.

    Edit:

    An easy way to check for boo by using the expression SFINAE:

      template & Lt; Typename T & gt; Constexpr Auto is_booable (int) - & gt; Fall (std :: declval & lt; T & gt; () .boo (), boole ()) {back true; } Template & lt; Typename T & gt; Constexpr bool is_booable (...) {return false; }   

    Usage:

      booable & lt; Is_booable & lt; Type & gt; (0) & gt; :: booIt (t);   

    More.

  • Comments

    Popular posts from this blog

    c - Mpirun hangs when mpi send and recieve is put in a loop -

    python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

    java - Unable to get JDBC connection in Spring application to MySQL -