标签: argument-dependent-lookup

ADL是否与命名的lambdas一起使用?

假设我在一个命名空间中有一个类和一个命名的lambda.

namespace bla {
    class X { /* ... */ };
    static auto lambda = []( X param ){ /* ... */ };
}
Run Code Online (Sandbox Code Playgroud)

这个lambda几乎等同于一个内联声明的函数.但是我可以从另一个不相关的命名空间调用lambda而不提及bla使用ADL 的命名空间(依赖于参数的查找,也称为Koenig查找)?

namespace blub {
    void f() {
        bla::X x;
        lambda( x ); // Does this compile?
    }
}
Run Code Online (Sandbox Code Playgroud)

c++ lambda argument-dependent-lookup c++11

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

类无法与不在其命名空间中的函数交朋友

我很难理解为什么以下MWE无法编译:

#include <iostream>

namespace N
{
    class Foo
    {
        friend void bar( Foo& f );
        void print(){ std::cout << "..." << std::endl; }    // private by default
    };  
}

void bar( N::Foo& f )
{
    f.print();
}

int main()
{
}
Run Code Online (Sandbox Code Playgroud)

g ++ 4.8.2错误

Test.cpp: In function ‘void bar(N::Foo&)’:
Test.cpp:8:8: error: ‘void N::Foo::print()’ is private
   void print(){ std::cout << "..." << std::endl; } // private by default
        ^
Test.cpp:14:10: error: within this context
Run Code Online (Sandbox Code Playgroud)

我几乎肯定在这里遗漏了一些东西,但肯定朋友的功能bar()可以访问该类的任何私人成员N::Foo.

注意:

  1. 移动 …

c++ namespaces argument-dependent-lookup

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

命名空间限定'运算符=='的重载

我很好奇以下为什么不编译:

#include <iostream>
#include <functional>

namespace Bar {
struct Foo {
  int x;
};
}  // Namespace                                                                                                                     

static bool operator==(const Bar::Foo& a, const Bar::Foo& b) {
  return a.x == b.x;
}

int main() {
  Bar::Foo a = { 0 };
  Bar::Foo b = { 1 };

  // The following line is OK
  std::cout << (a == b) << std::endl;

  // The following line is not OK
  std::cout << std::equal_to<Bar::Foo>()(a, b) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下编译器barfs:

[test]$ g++ --std=c++11 -o test test.cc …
Run Code Online (Sandbox Code Playgroud)

c++ argument-dependent-lookup c++11

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

调用ADL时是否需要表达式和命名空间之间的冲突?

给出以下示例,我希望解析器将std识别为函数:

#include <algorithm>

namespace test
{
    class foo{};

    void std(foo f);
}

int main()
{
    std(test::foo());
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是,使用GCC 4.8.4会导致错误 - "错误:意外命名空间名称'std':预期表达式."

使用clang 5.0,我得到"错误:意外命名空间名称'std':期望表达式"

这是预期的吗?我无法想象解析器在这里没有足够的上下文来区分表达式和命名空间?

编辑:示例使用应该调用ADL的更复杂的类型.在我的实际用例中,代码是通用的,我需要ADL.

c++ argument-dependent-lookup c++11

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

Rust 的 C++ ADL 重载函数的替代方案?

有一个库提供了一个通用函数和一些要使用的实现:

#include <iostream>

namespace lib {
  struct Impl1 {};
  struct Impl2 {};

  void process(Impl1) { std::cout << 1; }
  void process(Impl2) { std::cout << 2; }

  template<typename T> void generalize(T t) { process(t); }
}
Run Code Online (Sandbox Code Playgroud)

我想通过外部代码扩展它。以下是 C++ 允许这样做的方式:

#include <lib.h> // the previous snippet

namespace client {
  struct Impl3 {};

  void process(Impl3) { std::cout << 3; }
}

int main() { // test
  lib::generalize(client::Impl3{}); // it couts 3
}
Run Code Online (Sandbox Code Playgroud)

注意:lib的代码对 的一无所知,client并且不执行动态调度。如何在我的 Rust 代码中实现相同的目标?(如果我不能,是否有类似计划?)

overloading generic-programming open-closed-principle rust argument-dependent-lookup

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

为什么 ADL 在依赖类型名上失败?

我注意到get<0>(t)无法解析std::get<0>(t)其参数类型是否依赖于模板参数:

template <typename T>
void foo(T) {
  std::tuple<T> tup{0};
  get<0>(tup) = 1; // error: 'get' was not declared in this scope; did you mean 'std::get'?
}
Run Code Online (Sandbox Code Playgroud)

foo如果不是模板(即如果tup声明为std::tuple<int>)或使用 ,则效果很好using std::get;。难道它不应该在 ADL 通常特别有用的函数模板中工作吗?

c++ argument-dependent-lookup

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

当基类在 C++ 中具有交换函数时,为什么名称查找找不到 std::swap?

我有我代码中内容的副本

#include <utility>
using namespace std;

class Parent
{
    public:
        void swap(Parent*);
};
class A : public Parent 
{
    public:
        void handle();
};


void A::handle()
{

    long x = 1;
    long y = 2;
    swap(x,y);
}


int main()
{
    A v;
    v.handle();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

错误日志:

main.cpp: In member function 'void A::handle()':
main.cpp:21:10: error: no matching function for call to 'A::swap(long int&, long int&)'
  swap(x,y);
          ^
main.cpp:7:8: note: candidate: void Parent::swap(Parent*)
   void swap(Parent*);
        ^
main.cpp:7:8: note:   candidate expects 1 argument, …
Run Code Online (Sandbox Code Playgroud)

c++ name-lookup argument-dependent-lookup c++11

-2
推荐指数
1
解决办法
90
查看次数