Conditional Compile using Boost type-traits

后端 未结 3 1944
感情败类
感情败类 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: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::result returns something alike Boolean/Boolean:

    template
    class foo
    {
        void do_something(T obj, Boolean /*is_pod*/)
        {
          // do something for simple types
        }
        void do_something(T obj, Boolean /*is_pod*/)
        {
          // do something for classes/structs
        }
    
        void bar(T obj)
        {
           do_something(obj, IsPod::result());
        }
    }
    

提交回复
热议问题