[C ++ 11] Checking whether two types of instances can use the equal operator during compilation

Source: Internet
Author: User

The title looks very long,

I mainly want to have two types of variables A and B, respectively. can I obtain a bool constant during compilation to show whether I have an operation like a = B.

 #include <type_traits>                  template<             typename =                 static_cast<Dest>                 std::declval<Src>()))>           __test(          template<typename, typename>                           template<typename TA, typename TB>          __implicit_conv =                   __static_castable::__test<                          std::declval<TA>                      == std::declval<TB>                 ), >(           template<             typename =                 std::declval<TA>              == std::declval<TB>())>          __implicit_conv<TA, TB> __test(          template<typename, typename>                 template<typename A, typename B>      has_equal_operator = std::is_same<         decltype(__has_equal_operator_impl::__test<A, B>())> }

Then let's take a look at the test results.

      #include <iostream>   template<typename A, typename B>       std::cout <<         << bw::has_equal_operator<A, B>::value <<                 == ( A&,  B& A  == ( A&,  C& D  == ( B&,  C&             output_result<A, B>          output_result<B, A>          output_result<A, C>          output_result<B, C>          output_result<, >          output_result<, >          output_result<, >          output_result<decltype(std::cout), >       }

What is the use of this? 0,

You will find that if you write something similar to delegate, there will be a lot of compilation errors when you delete a function (for example, a function like this does not have a comparison operator,

With this, we can provide different comparison behaviors based on whether the = Operator exists. If yes, we can use it. If no = Operator exists, we can use our own defined comparison behaviors (such as comparing memory or directly true)

Although the semantics cannot be guaranteed, it is better in some cases (such as delegate.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.