假设我在一个命名空间中有一个类和一个命名的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) 我很难理解为什么以下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.
注意:
我很好奇以下为什么不编译:
#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) 给出以下示例,我希望解析器将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.
有一个库提供了一个通用函数和一些要使用的实现:
#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
我注意到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 通常特别有用的函数模板中工作吗?
我有我代码中内容的副本
#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)