小编And*_*rew的帖子

为什么C++参数范围会影响命名空间中的函数查找?

这看起来有点落后于我,但它有效:

#include <iostream>

namespace nTest
{
  struct cTest {};

  void fTest(cTest& x)
  {
    std::cout << "nTest::fTest(cTest&) called" << std::endl;
  }
}

int main(void)
{
  nTest::cTest x;
  fTest(x); //Weird! fTest is resolved since its parameter belongs to nTest.
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

通常,您需要nTest ::才能访问fTest,但其属于nTest的参数似乎将nTest添加到搜索fTest的可能范围列表中.我觉得参数范围影响函数查找似乎很奇怪.

这在GCC编译得很好,但我想知道这种用法是否可移植?这个范围机制的官方定义是什么?

c++ parameters portability scope namespaces

5
推荐指数
2
解决办法
1364
查看次数

标签 统计

c++ ×1

namespaces ×1

parameters ×1

portability ×1

scope ×1