相关疑难解决方法(0)

什么是"Argument-Dependent Lookup"(又名ADL或"Koenig Lookup")?

什么是关于什么参数依赖查找的好解释?很多人也称它为Koenig Lookup.

我最好知道:

  • 为什么这是好事?
  • 为什么这是一件坏事?
  • 它是如何工作的?

c++ c++-faq name-lookup argument-dependent-lookup

163
推荐指数
4
解决办法
3万
查看次数

为什么我要编写std :: cout而不是std :: <<

为什么我必须写,std::cout而不是std::<<像这样的代码行:

#include <iostream>

int main() {
    std::cout << "Hello, world!";
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

cout来自std库,<<通常不习惯进行位移?那么,为什么我::之前也不必编写范围运算符<<,因为它也用于其他含义?编译器如何知道后std::cout,<<意味着另一件事?

c++

69
推荐指数
2
解决办法
2234
查看次数

获取模板类对象的地址会导致模板参数的完全实例化

我用g ++ 4.6和4.8编译了这段代码的错误.g ++ 4.2和4.4就可以了.这是一个bug还是一些新的语言功能?

template <typename T>
struct A { typedef typename T::value_type type; };

template <typename U>
struct B
{
  void bar () { }
  void foo ()
  {
    // OK
    this->bar ();

    // OK
    (*this).bar ();

    // Error in g++ 4.6-4.8 
    // leads to full instantiating of template arg "U"
    (&*this)->bar ();
  }
};

int main ()
{
  B< A<void> > b;
  b.foo ();
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

g ++ inst.cc

inst.cc: In instantiation of ‘struct A<void>’:
inst.cc:20:5: …
Run Code Online (Sandbox Code Playgroud)

c++ gcc templates instantiation

9
推荐指数
1
解决办法
489
查看次数

为什么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
查看次数