我是否可以使用decltype(或类似的东西)进行显式模板实例化而无需签名重复?

ein*_*ica 4 c++ templates instantiation decltype c++11

我想实例化

template<typename T> void foo(
    T& t,
    SomeType some_parameter,
    AnotherType another_parameter,
    EtcType yet_another_parameter,
    AsYouCanTell this_is_a_very_long_signature);
Run Code Online (Sandbox Code Playgroud)

也就是说,具有长签名的函数.现在,我知道如何做到这一点:

template void foo<int>(
    int& t,
    SomeType some_parameter,
    AnotherType another_parameter,
    EtcType yet_another_parameter,
    AsYouCanTell this_is_a_very_long_signature);
Run Code Online (Sandbox Code Playgroud)

但我必须复制签名.此外,如果想要5种不同类型的特定实例化,我会复制5次吗?没有意义......

我想也许我可以写

template decltype(foo<int>);
Run Code Online (Sandbox Code Playgroud)

但由于某种原因,这不起作用.为什么?

5go*_*der 6

它实际上有效但语法不同:

template
decltype(foo<int>) foo<int>;
Run Code Online (Sandbox Code Playgroud)

decltype为您提供一个类型,但显式实例化需要一个声明 ,该声明后跟一个名称.

试过GCC 4.9.1; 它按预期工作,即使有-pedantic旗帜也没有任何警告编译.

  • @dyp那个规则交叉引用[dcl.fct.def],[dcl.fct.def.general]/p1中的语法基本上说它必须有一个body或`= default;`或`= delete;`来算作函数定义. (2认同)