消除了跨翻译单元使用的模板类型说明符

时间:2019-04-16 14:39:10

标签: template-specialization template-aliases specifier translation-unit

我想在翻译单元之间重用日志记录调用,以使fx INFOLOG< TU_T1 >();INFOLOG< TU_T2 >();两者都可以变成INFOLOG();。我会在tu1.cpp和tu2.cpp中放置不同的模板专长。

用例是这样,头文件可以跨翻译单元使用,同时对cpp文件应用不同的实现。但是,如果我必须在调用中包括类型说明符,则会破坏调用签名,并且.h不能重复使用。

我尝试使用模板别名,但无法对其进行编译。

尝试1:模板化参数

template< typename TU_T >
void INFOLOG();

template<> void INFOLOG< TU1 >()
{} // specialization in tu1.cpp compiles OK

template<> void INFOLOG< TU2 >()
{} // specialization in tu2.cpp compiles OK

INFOLOG< TU1 >(); // call in tu1 works, but tu2 can't call tu2's specialization.

尝试2:模板别名(使用伪代码来了解这个想法)

???? using INFOLOGBOTH = INFOLOG< TU1 >; // no compile in tu1.cpp or tu1.h

???? using INFOLOGBOTH = INFOLOG< TU2 >; // no compile in tu2.cpp or tu2.h. 

我缺少什么方法或语法?

0 个答案:

没有答案
相关问题