Conditional Compile using Boost type-traits

后端 未结 3 1874
感情败类
感情败类 2021-02-06 09:49

I have a template that I would like to conditionally compile depending on the type of the argument. I only care about differentiating between \"Plain Old Data\" (POD), i.e., in

相关标签:
3条回答
  • 2021-02-06 10:10

    You can do it without enable_if, because all you need is to dispatch depending on type traits. enable_if is used to add/remove template instantiations to/from overload resolution. You may want to use call traits to choose the best method to pass objects to your function. As a rule, objects should be passed by reference, whereas POD is passed by value. call_traits let's you choose between const and non-const references. The code below uses const reference.

    #include <boost/type_traits.hpp>
    #include <boost/call_traits.hpp>
    
    template <typename T>
    class foo {
    public:
        void bar(typename boost::call_traits<T>::param_type obj) {
            do_something(obj, boost::is_pod<T>());
        }
    private:
        void do_something(T obj, const boost::true_type&)
        {
          // do something for POD
        }
        void do_something(const T& obj, const boost::false_type&)
        {
          // do something for classes
        }
    };
    
    0 讨论(0)
  • 2021-02-06 10:10

    Using preprocessor here is not possible. Have a look at Boost Enable If library instead.

    Specifically, in your case it would look like (not tested):

    void bar (typename enable_if <is_pod <T>, T>::type do_something)
    {
        // if is POD
    }
    
    void bar (typename disable_if <is_pod <T>, T>::type do_something)
    {
        // if not
    }
    
    0 讨论(0)
  • 2021-02-06 10:21

    You can't solve this with the preprocessor, since it doesn't know about C++. (It's a dumb text replacement tool.) Use templates to do this.

    Assuming IsPod<T>::result returns something alike Boolean<true>/Boolean<false>:

    template<T>
    class foo
    {
        void do_something(T obj, Boolean<true> /*is_pod*/)
        {
          // do something for simple types
        }
        void do_something(T obj, Boolean<false> /*is_pod*/)
        {
          // do something for classes/structs
        }
    
        void bar(T obj)
        {
           do_something(obj, IsPod<T>::result());
        }
    }
    
    0 讨论(0)
提交回复
热议问题