0

Sometimes I like my template class method definitions in a source file (or second header) to reduce build times. In all my source files I use a type alias for the class I'm defining the methods of to keep repetitions and typing time at a minimum.

Simple example:

// Foo.h
class Foo
{ void bar(); }
// Foo.cpp
using _ = Foo;

void _::bar()
{}

I have done this for template classes, which I want to keep the template parameter open of in method definitions, using MSVC.

// Foo.h
template<typename T>
class Foo
{ void bar(); }
// Foo.cpp
template<typename T>
using _ = Foo<T>;

template<typename T>
void _<T>::bar()
{}

However, while MSVC accepts this, GCC (12.2.0, MinGW) doesn't (invalid use of incomplete type). Am I maybe just doing it the wrong way or is it just not possible?

Neonit
  • 680
  • 8
  • 27
  • 1
    Do note that the name `_` in the global scope is reserved for the implementation: https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier – NathanOliver Feb 09 '23 at 16:16
  • 1
    *"typing time at a minimum"*. Most IDEs provide auto completion, so you don't have to **type** the full name... – Jarod42 Feb 09 '23 at 16:22
  • 1
    And most IDEs also provide a way to generate method definition from declaration... – Yksisarvinen Feb 09 '23 at 16:25
  • Have you actually measured an improvement of build times. Personally I'd need a good reason to reduce maintainability of my code like this. Furthermore especially with templates this is likely to result in a name conflict anyways: In most cases the implementation of a template needs to be part of a header file which means you'll end up with a name conflict when including the template header in a translation unit implementing some other functionality using your `_`-"trick" – fabian Feb 09 '23 at 18:34
  • @fabian In general, splitting code into header and source files has reduced my build times, at least with MSVC. I'm just applying this scheme to template classes as well. I feel this is a *necessary impact* on the *maintainability*. After all, you require separate source files for certain cases, e.g. static initialization or to resolve dependency cycles, anyway. I'm currently switching to GCC and until now get the impression that GCC is faster than MSVC. I think, I haven't used the `_` shortcut in (secondary) header files. Only in source files. – Neonit Feb 10 '23 at 07:26

0 Answers0