2

我知道使用 type alias 和 typedef 可以使代码更易读、更不容易出错并且易于修改。但是在这个例子中我想知道这个复杂的类型:

#include <iostream>
#include <typeinfo>

char ( * (* x() )[] )();

int main(){

    std::cout << typeid(x).name() << std::endl;
}

正如经验法则所说,我从内到外阅读它,但我觉得它太混乱了。

有人可以帮我怎么读吗?

GCC的输出:

FPA_PFcvEvE
4

2 回答 2

5
char        - returning a char                         *
  (*        - of pointers to                    *---v  ^
    (*      - returning a pointer to    *---v   ^   v  ^
       x()  - a function `x`          --^   v   ^   v  ^
    )[]     - an array                      *---^   v  ^
  )();      - functions                             *--^

并查看https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangle.array-type,例如:

FPA_PFcvEvE
^           - function (
 ^          - returning pointer
  ^         - to an array [
  ><        - with no dimension expression
   ^        - separator, end of array ]
    ^       - of pointers
     ^      - to functions (
      ^     - returning char
       ^    - (void) functions that take no arguments
        ^   - separator, end of function )
         ^  - (void) it's a function that takes on arguments
          ^ - separator, end of function )

例子:

#include <iostream>

char return_a() { return 'a'; }
char return_b() { return 'b'; }
char return_c() { return 'c'; }

char (* array_of_funcs_that_return_char[3])() = {
    return_a,
    return_b,
    return_c,
};

char (* (*pointer_to_array_of_funcs_that_return_char)[3])() = &array_of_funcs_that_return_char;

char ( * (* x() )[3] )() {
    return pointer_to_array_of_funcs_that_return_char;
}

int main() {
    std::cout << (*x())[1](); // outputs 'b'
}
于 2021-07-24T11:59:19.667 回答
3

我制作了一个小型库来帮助将复杂类型解码为英语标准 std::string 值。只需调用type_to_string::describe<T>()它,它将返回一个(大部分)人类可读的描述类型 T 的字符串。

这是您的类型:

// url-includes works with compiler explorer, not normal c++
#include <https://raw.githubusercontent.com/cuzdav/type_to_string/main/describe.hpp>

#include <iostream>
int main() {
    using X = char ( * (*() )[] )();
    std::cout << type_to_string::describe<X>() << std::endl;
}

它的输出是:

function taking (), returning pointer to array[] of pointer to function taking (), returning char

现场观看 https://godbolt.org/z/6Kjf5jb7E

(实现比作为 SO 答案的一部分有用的更详细,但它可以在我的 github 上的链接上阅读。)

于 2021-07-24T13:47:14.627 回答