此代码行末尾的方括号是什么意思
What do the square brackets at the end of this code line mean?
我对C++相当陌生,在遇到一个参考站点时,我遇到了以下代码片段,从那以后我一直在努力分解它。
这就是我仍在努力弄清楚的:
int (*(*callbacks[5])(void))[3]
我已经通读了部分C++帮助书籍,并且(大致(了解优先级流程。但是看到许多操作员聚集在一起让我很失望,我相当困惑。我看过其他的例子和解释(在这里(,但右边额外的 [3] 下标只会让我的事情复杂化。
我想知道我应该如何处理这种复杂的代码,即我从哪里开始,我应该遵循什么顺序等。
真的很感激你的帮助!谢谢!!
这是一个包含 5 个指针的数组,指向不带参数的函数,并返回指向 3 个整数数组的指针。
我只是因为 cdecl.org 告诉我才想通的。 这是typedef使事情更容易阅读的地方之一:
typedef int (*PointerToArrayOfInts)[3];
typedef PointerToArrayOfInts (*FunctionReturningPointerToArray)(void);
FunctionReturningPointerToArray callbacks[5];
这是一个原始的内省"漂亮打印机"的开始,它让C++编译器本身为您分解内容:
#include <iostream>
template <typename T>
struct introspect;
template <>
struct introspect<int> {
static std::ostream& prettyPrint(std::ostream& os) { return os << "int"; }
};
template <typename T>
struct introspect<T*> {
static std::ostream& prettyPrint(std::ostream& os) {
os << "pointer to ";
return introspect<T>::prettyPrint(os);
}
};
template <typename T, std::size_t N>
struct introspect<T[N]> {
static std::ostream& prettyPrint(std::ostream& os) {
os << "array of " << N << " (";
introspect<T>::prettyPrint(os);
return os << ")";
}
};
template <typename Res>
struct introspect<Res(void)> {
static std::ostream& prettyPrint(std::ostream& os ) {
os << "function returning (";
introspect<Res>::prettyPrint(os);
return os << ")";
}
};
int main() {
int (*(*callbacks[5])(void))[3];
introspect<decltype(callbacks)>::prettyPrint(std::cout) << 'n';
return 0;
}
输出:
array of 5 (pointer to function returning (pointer to array of 3 (int)))
int (*(*callbacks[5])(void))[3]
将回调声明为指向函数的指针的数组 5 (void( 返回指向 int 的数组 3 的指针
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- C++避免重复声明的语法是什么
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- C++从另一个类访问公共静态向量的正确方法是什么
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- C++中名称篡改的目的是什么
- 在 c++ 中拥有一组结构的正确方法是什么?
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 是什么阻止DOMTimerCoordinator::NextID进入无休止的循环
- 派生类销毁的最佳实践是什么
- 这个语法std::class<>{}(arg1, arg2) 在C++中是什么意思?
- 通过JNI传递数据数组的最快方法是什么
- "using namespace std;"在C++的作用是什么?
- 在两台机器之间进行时间戳的最佳c++chrono函数是什么
- 方括号在"map <string, int> instance[numberFeatures];"中是什么意思
- 此代码行末尾的方括号是什么意思
- 在以下代码中,该方括号的用途是什么?
- 数组在方括号中增加元素是什么意思
- c++中指针类中的[]方括号是什么意思?