decltype(&normal_func)`和decltype`(normal_func)之间的差异

问题描述 投票:0回答:1

给出声明void close_file_func(std::FILE* fd){},我发现decltype(&close_file_func)void (*)(_IO_FILE*)的类型,而decltype(close_file_func)void (_IO_FILE*)的类型?

您可以检查相关代码槽https://godbolt.org/z/QK5q3o

为了方便起见,我在下面发布代码和执行输出。

#include<iostream>
#include<typeinfo>

void close_file_func(std::FILE* fd);

int main()
{
    std::cout << typeid(close_file_func).name() << std::endl;
    std::cout << typeid(&close_file_func).name() << std::endl;
}

//Terminate outputs:
//FvP8_IO_FILEE
//PFvP8_IO_FILEE

[echo 'PFvP8_IO_FILEE' | c++filt -tvoid (*)(_IO_FILE*)

[echo "FvP8_IO_FILEE" | c++filt -tvoid (_IO_FILE*)

我想了一遍又一遍,但我仍然听不懂。对于这个问题的任何提示,我将不胜感激。

c++ c++11 decltype
1个回答
0
投票

与实体一起使用时,decltype将产生实体的类型;因此decltype产生函数类型,即decltype(close_file_func)

1)如果参数是未括号化的id表达式或未括号化的类成员访问表达式,则decltype会产生该表达式命名的实体的类型。

[void (_IO_FILE*)获取函数的地址并返回函数指针,然后&close_file_func将产生函数指针类型,即decltype(&close_file_func)

© www.soinside.com 2019 - 2024. All rights reserved.