Is this a legal approach to template definition separation

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

JOAQUIN LOPEZ MU?Z wrote:
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,;)
If body has unprotected commas, this definition won't work. Jonathan

JOAQUIN LOPEZ MU?Z wrote:
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
foo.hpp is not a translation unit, as I understand it... could you please explane were is a place for reduce of a compile time in your approach?
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
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

"Oleg G. Abrosimov" ha escrito:
JOAQUIN LOPEZ MU?Z wrote:
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
foo.hpp is not a translation unit, as I understand it... could you please explane were is a place for reduce of a compile time in your approach?
The reduction would take effect in any translation unit including foo.hp: //**************** bar.cpp ********** #include<foo.pp> // use foo //********************** (on a side note, Jonathan pointed out that the macro mechanism dows not work, but my question holds regardless of the particular implementation technique.) Joaquín M López Muñoz
participants (4)
-
JOAQUIN LOPEZ MU?Z
-
Joaquín Mª López Muñoz
-
Jonathan Turkanis
-
Oleg G. Abrosimov