使用 libtorch,以下代码片段会导致错误:
#include <iostream>
#include <torch/torch.h>
using namespace torch;
int main() {
std::cout << zeros({}) << std::endl;
}
编译器抱怨:
% g++ -lc10 -ltorch -ltorch_cpu test.cpp -o test.x
test.cpp: In function ‘int main()’:
test.cpp:5:23: error: call of overloaded ‘zeros(<brace-enclosed initializer list>)’ is ambiguous
5 | std::cout << zeros({}) << std::endl;
| ~~~~~^~~~
In file included from /opt/links/include/ATen/Functions.h:1334,
from /opt/links/include/ATen/ExpandUtils.h:4,
from /opt/links/include/torch/csrc/autograd/input_metadata.h:3,
from /opt/links/include/torch/csrc/autograd/function.h:7,
from /opt/links/include/torch/csrc/autograd/custom_function.h:7,
from /opt/links/include/torch/csrc/api/include/torch/autograd.h:5,
from /opt/links/include/torch/csrc/api/include/torch/all.h:7,
from /opt/links/include/torch/csrc/api/include/torch/torch.h:3,
from test.cpp:2:
/opt/links/include/ATen/ops/zeros.h:35:19: note: candidate: ‘at::Tensor at::zeros(IntArrayRef, c10::TensorOptions)’
35 | inline at::Tensor zeros(at::IntArrayRef size, at::TensorOptions options={}) {
| ^~~~~
In file included from /opt/links/include/torch/csrc/api/include/torch/types.h:7,
from /opt/links/include/torch/csrc/api/include/torch/data/dataloader_options.h:4,
from /opt/links/include/torch/csrc/api/include/torch/data/dataloader/base.h:3,
from /opt/links/include/torch/csrc/api/include/torch/data/dataloader/stateful.h:4,
from /opt/links/include/torch/csrc/api/include/torch/data/dataloader.h:3,
from /opt/links/include/torch/csrc/api/include/torch/data.h:3,
from /opt/links/include/torch/csrc/api/include/torch/all.h:9:
/opt/links/include/torch/csrc/autograd/generated/variable_factories.h:565:19: note: candidate: ‘at::Tensor torch::zeros(at::IntArrayRef, c10::TensorOptions)’
565 | inline at::Tensor zeros(at::IntArrayRef size, at::TensorOptions options = {}) {
| ^~~~~
为什么
at::zeros
被列入候选人而我却不是using namespace at
?
还有很多其他关于此类错误的问题,我认为这些问题不能回答我的问题:
using
的,另一个是现在位于该命名空间中的,而在我的情况下,所声明的候选者之一似乎不应该在我的范围内zeros
更改为 torch::zeros
会使文件编译还有很多很多;我承认我在这一点上感到沮丧并放弃了查看它们。