为什么typeid on function是正常的,而sizeof则没有

Smi*_*les 1 c++ syntax function sizeof typeid

发现这个时,在C++中鬼混:

#include <iostream>
#include <typeinfo>
#include <boost/core/demangle.hpp>

int main(int argc, char *argv[]) {
    using namespace std;
    using boost::core::demangle;

    cout 
        << demangle(typeid(void()).name()) << endl // ok so far
        << sizeof(void()) << endl; // error here: invalid application of 'sizeof' to a function type
}
Run Code Online (Sandbox Code Playgroud)

我无法理解为什么我可以获得函数类型的type_info,但不能获得它的大小.
关于C++如何看待函数的一些上下文可能会有所帮助.

Jam*_*nze 6

主要原因很简单:在C++中,函数有一个类型,但它没有大小.请记住,在C++中,某些内容的大小是您必须添加到指针以获取表中下一个元素的字节数.由于您无法将函数放在表中,因此它们没有大小.