Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional Compile using Boost type-traits

Tags:

c++

boost

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., integers, etc or classes/structs. I'm using c++ VS2008 on Windows.

template<T>
class foo
{
    void bar(T do_something){
    #if IS_POD<T>
        do something for simple types
    #else
        do something for classes/structs
    #endif
}}

I've been looking at the boost library's and I can see that they appear to have what I want. However, I do not understand what the correct syntax for the #if statement would be.

Any help would be appreciated.


Edit --- After reading the responses, I see I overlooked something in my definition of the question. Class foo is a templated class that only needs to instance the version of bar that is correct for class type T. I was looking for a solution that can be resolved a compile time. Hope this clears up my problem.

like image 672
photo_tom Avatar asked Jul 29 '10 20:07

photo_tom


2 Answers

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
    }
};
like image 140
user401947 Avatar answered Nov 05 '22 00:11

user401947


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());
    }
}
like image 39
sbi Avatar answered Nov 05 '22 00:11

sbi