C++调用和定义不匹配
C++ call and definition mismatch
我正在查看此代码块 https://github.com/pytorch/pytorch/blob/master/torch/csrc/autograd/profiler.cpp#L141
pushCallback(
[config](const RecordFunction& fn) {
auto* msg = (fn.seqNr() >= 0) ? ", seq = " : "";
if (config.report_input_shapes) {
std::vector<std::vector<int64_t>> inputSizes;
inputSizes.reserve(fn.inputs().size());
for (const c10::IValue& input : fn.inputs()) {
if (!input.isTensor()) {
inputSizes.emplace_back();
continue;
}
const at::Tensor& tensor = input.toTensor();
if (tensor.defined()) {
inputSizes.push_back(input.toTensor().sizes().vec());
} else {
inputSizes.emplace_back();
}
}
pushRangeImpl(fn.name(), msg, fn.seqNr(), std::move(inputSizes));
} else {
pushRangeImpl(fn.name(), msg, fn.seqNr(), {});
}
},
[](const RecordFunction& fn) {
if (fn.getThreadId() != 0) {
// If we've overridden the thread_id on the RecordFunction, then find
// the eventList that was created for the original thread_id. Then,
// record the end event on this list so that the block is added to
// the correct list, instead of to a new list. This should only run
// when calling RecordFunction::end() in a different thread.
if (state == ProfilerState::Disabled) {
return;
} else {
std::lock_guard<std::mutex> guard(all_event_lists_map_mutex);
const auto& eventListIter =
all_event_lists_map.find(fn.getThreadId());
TORCH_INTERNAL_ASSERT(
eventListIter != all_event_lists_map.end(),
"Did not find thread_id matching ",
fn.getThreadId());
auto& eventList = eventListIter->second;
eventList->record(
EventKind::PopRange,
StringView(""),
fn.getThreadId(),
state == ProfilerState::CUDA);
}
} else {
popRange();
}
},
config.report_input_shapes);
这只有三个参数。但是 pushCallback 的定义似乎在这个位置 https://github.com/pytorch/pytorch/blob/master/torch/csrc/autograd/record_function.cpp#L35 并采用四个参数。
void pushCallback(
RecordFunctionCallback start,
RecordFunctionCallback end,
bool needs_inputs,
bool sampled) {
start_callbacks.push_back(std::move(start));
end_callbacks.push_back(std::move(end));
if (callback_needs_inputs > 0 || needs_inputs) {
++callback_needs_inputs;
}
is_callback_sampled.push_back(sampled);
if (sampled) {
++num_sampled_callbacks;
}
}
我不知道为什么该函数调用可以以这种方式工作。
如果你看一下标题,你会发现它是用 4 个参数声明的,其中最后三个参数有默认值:
TORCH_API void pushCallback( RecordFunctionCallback start, RecordFunctionCallback end = [](const RecordFunction&){}, bool needs_inputs = false, bool sampled = false);
默认参数仅出现在声明上,而不显示在定义上。
相关文章:
- 模板推导:为什么函数指针模板定义在常量和/或引用时不匹配?
- C++调用和定义不匹配
- Tbb 库:错误:编写自定义类函数而不是 lambda 表达式时,对函数的调用不匹配
- Template类定义中的Template方法与声明不匹配
- COM 互操作结构定义与内存布局不匹配
- 用户定义的构造函数重载与参数超类的重载不匹配
- Qt:"closeEvent"的外线定义与"MainWindow"中的任何声明都不匹配
- 类中的 C++ 函数调用与定义不匹配
- C 功能定义和可变声明不匹配
- C++模板方法定义在类中不匹配
- 为什么通用模板方法定义与模板类专用化不匹配?
- 函数声明和定义的返回类型不匹配,编译器可以吗?
- 'function'的外联定义与'Class'中的任何声明都不匹配
- C++,构造函数:'class'的外联定义与'class'中的任何声明都不匹配
- VC++编译器升级2010->2015重新定义; 'constexpr'说明符不匹配
- C++:使用constexpr对模板参数进行别名,导致函数定义与声明不匹配
- 函数定义与其在 C 中的声明不匹配,但在 C++ 中匹配
- 方法定义的类型推断与声明不匹配
- 为什么我的静态方法的返回值与定义的构造函数(在 c++ 中)之间存在类型不匹配?
- 由于函数指针类型定义与具有正确const逻辑的指定函数不匹配,导致编译警告