用decltype()声明一个函数签名

Lor*_*one 9 c++ definition decltype c++11

是否可以声明函数bar具有与函数相同的签名foo

int foo(int a)
{
    return 0; 
}

decltype(foo) bar
{
    return 1;
} //imaginary syntax
Run Code Online (Sandbox Code Playgroud)

dyp*_*dyp 2

我认为这同样适用于 typedef 和别名:您可以用来decltype声明函数,但不能定义它:

int foo();

decltype(foo) bar;

int foo()
{
    return bar();
}

int bar() { return 0; }
Run Code Online (Sandbox Code Playgroud)

被 clang++3.5 和 g++4.8.1 接受


[dcl.fct.def.general]/2 禁止(在语法上)定义不带括号的函数:

函数定义中的声明符应具有以下形式

       D1 ( 参数声明子句 ) cv-限定符-seq opt 引用限定符opt异常规范opt属性说明符-seq opt尾随返回类型opt

如 8.3.5 中所述。