为什么名称空间限定名称可以作为候选名称?

问题描述 投票:0回答:1

使用 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


还有很多其他关于此类错误的问题,我认为这些问题不能回答我的问题:

  • 错误:尝试定义带有和不带有可选/默认参数的函数
    不相关,因为:两个变体都是在同一名称空间中定义的
    问题:12
  • 错误:#include 了标头的 C 变体而不是 C++ 变体
    不相关,因为:据我所知,libtorch 没有 C 接口
    问题:12
  • 错误:在两个不同的命名空间中声明相同的函数(非常接近!)
    不相关,因为:两个命名空间都已纳入范围,一个是
    using
    的,另一个是现在位于该命名空间中的,而在我的情况下,所声明的候选者之一似乎不应该在我的范围内
    问题:12
  • 错误:所有可用的重载都没有正确的类型
    不相关,因为: 两个候选者具有相同的类型,并且与调用相匹配;例如,将
    zeros
    更改为
    torch::zeros
    会使文件编译
    问题:123

还有很多很多;我承认我在这一点上感到沮丧并放弃了查看它们。

c++ pytorch
1个回答
0
投票

因为 pytorch 标头包含一个

using namespace at;

指令。请参阅此处

© www.soinside.com 2019 - 2024. All rights reserved.