是否可以将函数 bar
声明为与函数 foo
具有相同的签名?
int foo(int a)
{
return 0;
}
decltype(foo) bar
{
return 1;
} //imaginary syntax
最佳答案
我认为这同样适用于 typedef 和别名:您可以使用 decltype
来声明一个函数,但不能定义它:
int foo();
decltype(foo) bar;
int foo()
{
return bar();
}
int bar() { return 0; }
被 clang++3.5 和 g++4.8.1 接受
[dcl.fct.def.general]/2 禁止(语法上)不带括号的函数定义:
The declarator in a function-definition shall have the form
D1 (
parameter-declaration-clause)
cv-qualifier-seqopt ref-qualifieropt exception-specificationopt attribute-specifier-seqopt trailing-return-typeoptas described in 8.3.5.
关于c++ - 使用 decltype() 声明函数签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21055039/