是否可以在不进行签名重复的情况下使用decltype(或类似方法)进行显式模板实例化? [英] Can I use decltype (or something similar) for explicit template instantiation without signature duplication?
问题描述
我要实例化
template<typename T> void foo(
T& t,
SomeType some_parameter,
AnotherType another_parameter,
EtcType yet_another_parameter,
AsYouCanTell this_is_a_very_long_signature);
即具有长签名的函数。现在,我知道该怎么做:
that is, a function with a long signature. Now, I know how to do this:
template void foo<int>(
int& t,
SomeType some_parameter,
AnotherType another_parameter,
EtcType yet_another_parameter,
AsYouCanTell this_is_a_very_long_signature);
但是我必须重复签名。此外,如果要针对5种不同类型的特定实例化该怎么做-我需要将其复制5次吗?没道理...
But I have to duplicate the signature. Also, what if want specific instantiation for 5 different types - do I copy it 5 times? Doesn't make sense...
我在想也许我可以写
template decltype(foo<int>);
但由于某些原因,此方法不起作用。为什么?
but for some reason this doesn't work. Why?
推荐答案
它确实有效,但是语法不同:
It actually works but the syntax is different:
template
decltype(foo<int>) foo<int>;
decltype
为您提供类型,但显式实例化需要一个声明,该类型后面是一个名称。
decltype
gives you a type but the explicit instantiation requires a declaration which is a type followed by a name.
尝试使用GCC 4.9.1;它可以按预期工作,即使使用 -pedantic
标志也可以进行编译,而不会发出任何警告。
Tried with GCC 4.9.1; it works as expected and compiles without any warnings even with the -pedantic
flag.
这篇关于是否可以在不进行签名重复的情况下使用decltype(或类似方法)进行显式模板实例化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!