Скажем, что мой код структурирован таким образом:
-
header1.h
template <class T, template<class> class C> struct metafunction { using type = typename C<T>::type; }; inline namespace msn { template <class T> struct implementation; } // uses the *implementation* not defined in the header! template <class T> struct use_case { using type = typename metafunction<T, implementation>::type; };
-
cpp1.cpp
#include <header1.h> // I'll only need this in this compilation unit, so the // question is: "Is this a good place to define it?" template <> struct implementation<int> { using type = int; }; int main() { using tt = int; // is this point of instantiation OK due to // the existence of a specialization in the same cpp file? using tt = use_case<int>::type; tt var; (void)var; }
Мое предварительное условие состоит в том, что я буду использовать только специальную специализацию внутри файлов cpp, поэтому мне не придется иметь дело с проблемами компоновщика. Я знаю, что это не будет работать для файла cpp2.cpp
, включая header1.h
, и пытается просто использовать use_case<int>
или переопределить implementation<int>
, который нарушает ODR. Так что я спрашиваю, является ли этот код аналогичным линейной форме (версия, где все помещается в один файл cpp с соответствующий порядок), который (по-видимому) компилируется штрафом.