Boost logo

Boost Users :

Subject: Re: [Boost-users] [TypeTraits] Is it possible to detect that typeis not exist and use default type?
From: Sakharuk, Vlad \(GMI Development\) (Vlad_Sakharuk_at_[hidden])
Date: 2009-06-30 15:53:21


Thanks!

        -----Original Message-----
        From: boost-users-bounces_at_[hidden]
[mailto:boost-users-bounces_at_[hidden]] On Behalf Of Roman
Perepelitsa
        Sent: Tuesday, June 30, 2009 3:32 PM
        To: boost-users_at_[hidden]
        Subject: Re: [Boost-users] [TypeTraits] Is it possible to detect
that typeis not exist and use default type?
        
        
        2009/6/30 Sakharuk, Vlad (GMI Development)
<Vlad_Sakharuk_at_[hidden]>
        

                Here is example:
                class one {
                    public:
                    typedef someclass internal;
                };
                 
                
                class two {
                // No internal class
                };
                 
                class default_internal{
                };
                 
                I would like to be able to write something if it
possible:
                 
                template<class T, class B = if_exist(T::internal)
T::internal else default_internal > class proc {
                };
                 
                So I can use proc with both
                 
                proc<one> One;
                
                proc<two> Two;
                 
                without specifying derived type?
                 

        #include <iostream>
        #include <boost/mpl/has_xxx.hpp>
        #include <boost/mpl/eval_if.hpp>
        #include <boost/mpl/identity.hpp>
        
        
        struct someclass {
          static void hello() {
            std::cout << "someclass" << std::endl;
          }
        };
        
        
        struct one {
         public:
          typedef someclass internal;
        };
         
        struct two {};
         
        struct default_internal {
          static void hello() {
            std::cout << "default_internal" << std::endl;
          }
        };
        
        
        BOOST_MPL_HAS_XXX_TRAIT_DEF(internal)
        
        
        
        // Here Internal is either T::internal
        // if it exists or default_internal.
        template <class T, class Internal>
        struct proc_impl {
          static void hi() {
            Internal::hello();
          }
        };
        
        
        template <class T>
        struct get_internal {
          typedef typename T::internal type;
        };
        
        
        template <class T>
        struct proc :
          proc_impl<
            T,
            typename boost::mpl::eval_if<
              has_internal<T>,
              get_internal<T>,
              boost::mpl::identity<default_internal>
>::type
> {
        };
        
        
        int main() {
          proc<one>::hi();
          proc<two>::hi();
        }

        Roman Perepelitsa.

--------------------------------------------------------------------------
This message w/attachments (message) may be privileged, confidential or proprietary, and if you are not an intended recipient, please notify the sender, do not use or share it and delete it. Unless specifically indicated, this message is not an offer to sell or a solicitation of any investment products or other financial product or service, an official confirmation of any transaction, or an official statement of Merrill Lynch. Subject to applicable law, Merrill Lynch may monitor, review and retain e-communications (EC) traveling through its networks/systems. The laws of the country of each sender/recipient may impact the handling of EC, and EC may be archived, supervised and produced in countries other than the country in which you are located. This message cannot be guaranteed to be secure or error-free. References to "Merrill Lynch" are references to any company in the Merrill Lynch & Co., Inc. group of companies, which are wholly-owned by Bank of America Corporation. Securities and Insurance Products: * Are Not FDIC Insured * Are Not Bank Guaranteed * May Lose Value * Are Not a Bank Deposit * Are Not a Condition to Any Banking Service or Activity * Are Not Insured by Any Federal Government Agency. Attachments that are part of this E-communication may have additional important disclosures and disclaimers, which you should read. This message is subject to terms available at the following link: http://www.ml.com/e-communications_terms/. By messaging with Merrill Lynch you consent to the foregoing.
--------------------------------------------------------------------------
 



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net