Boost logo

Boost :

From: JOAQUIN LOPEZ MU?Z (joaquin_at_[hidden])
Date: 2005-01-25 16:12:41


This is not strictly related to Boost, but its
motivation comes from an effort to reduce compilation
times for a Boost library, so I'll risk posting the
question.
Consider the following:

// *************** some_class_template.hpp ***************

#ifdef INCLUDE_DEFINITION
# define BODY_INCLUDED 1
#else
# define BODY_INCLUDED 0
#endif

#define BODY_DEFINITION(body) BOOST_PP_IF(BODY_INCLUDED,body,;)

template<typename ...>
class some_template_class
{
  ...
  void f(...)
  BODY_DEFINITION((
  {
    // implementation of f goes here
  }
  ))
  ...
};

// *************** foo.hpp ***************

#include "some_class_template.hpp"

class foo
{
  void bar();

private:
  some_class_template<...> m;
};

// *************** foo.cpp ***************

#define INCLUDE_DEFINITION
#include "user.hpp"

void foo::bar()
{
  m.f(); // f is defined, OK to use it.
}

Well, the idea is to generate in a simple way a
declaration only header of a template class from the full
definition, for those situations when the definitions are
not needed. The purpose is to reduce compilation times in
those translations units including foo.hpp, as they
won't be using the class template directly.
Other, more conventional approaches, like having the
definitions of the class template out of line in a
separate file, are harder to maintain and proved to
be problematic with buggy compilers (MSVC++ 6.5.)
Yet another alternative is to use the pimpl idiom, but
this has a runtime penalty.

My question is: is this ODR compliant? Anyone sees a
problem with this approach? Thank you,

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk