c++ - 使用 decltype() 声明函数签名

标签 c++ c++11 definition decltype

是否可以将函数 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-typeopt

as described in 8.3.5.

关于c++ - 使用 decltype() 声明函数签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21055039/

相关文章:

c++ - 运算符 << 重载错误 - 未找到运算符

c++ - 如果您始终将其存储在 shared_ptr 中,您的接口(interface)是否需要虚拟析构函数?

C++ *this 的值类别是什么?

scala - 什么是全动力闭合?

c++ - Boost ASIO 将 X 字节同步读入 vector

c++ - 使用此函数查找数组的长度

c++ - typedef 函数指针的容器

c++ - 定义静态成员的默认参数

language-agnostic - 什么是幂等操作?

c - 将一个数分解为素数的乘积并像 18=2*3^2 这样打印