- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在下面重现了 Stroustrup 书(第 4 版)第 396 和 397 页中给出的参数相关查找 (ADL) 示例:
namespace N {
struct S { int i; };
void f(S);
void g(S);
void h(int);
};
struct Base {
void f(N::S);
};
struct D : Base {
void mf(N::S);
void g(N::S x)
{
f(x); // call Base::f()
mf(x); // call D::mf()
h(1); // error: no h(int) available
}
};
上面的评论是正确的(我已经测试过了),但这似乎与作者在下一段中所说的不一致:
In the standard, the rules for argument-dependent lookup are phrased in terms of associated namespaces (iso §3.4.2). Basically:
- If an argument is a class member , the associated namespaces are the class itself (including its base classes) and the class's enclosing namespaces.
- If an argument is a member of a namespace, the associated namespaces are the enclosing namespaces.
- If an argument is a built-in type, there are no associated namespaces.
在示例中,类型为N::S
的x
不是D
类的成员,也不是其基类基础
。但它是 namespace N
的成员。根据上面的第二个项目符号,函数 N::f(S)
应该被调用,而不是 Base::f()
。
上面的结果似乎也不符合标准中第 3.4.2p2 段中的第二个项目符号,它说:
If T is a class type (including unions), its associated classes are: the class itself; the class of which it is a member, if any; and its direct and indirect base classes. Its associated namespaces are the namespaces of which its associated classes are members. Furthermore, if T is a class template specialization, its associated namespaces and classes also include: the namespaces and classes associated with the types of the template arguments provided for template type parameters (excluding template template parameters); the namespaces of which any template template arguments are members; and the classes of which any member templates used as template template arguments are members.
最佳答案
3.4.2/3 Let
X
be the lookup set produced by unqualified lookup (3.4.1) and letY
be the lookup set produced by argument dependent lookup (defined as follows). IfX
contains
- a declaration of a class member, or
- a block-scope function declaration that is not a using-declaration, or
- a declaration that is neither a function or a function template
then Y is empty. Otherwise...
所以基本上,当普通查找找到成员函数或局部( block 作用域)函数声明(或不是函数的东西)时,ADL 不会启动。当普通查找找到一个独立的命名空间作用域函数时,或者当它什么也没找到时,它确实会启动。
关于c++ - 我需要对 Stroustrup 关于 ADL 的新书上的这个例子做一些澄清,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19209453/
练习 13,来自 Stroustrup 的编程原则和使用 c++ 的实践的第 12 章。 A superellipse is a two-dimensional shape defined by th
在《编程:使用 C++ 的原理与实践》的第 5.10.1 章中,有一个“试试这个”练习,用于调试某个区域的错误输入。前置条件是长度和宽度的输入是否为 0 或负数,而后置条件是检查面积是否为 0 或负数
这些是 Stroustrup 在他最新的 C++ 书中给出的指导方针在函数中传递参数: [1] Use pass-by-value for small objects. [2] Use pass-by
在 Stroustrup 的 C++ 编程语言书(第 3 版)中,在数值章节中,他展示了以下代码片段: void f(valarray& d) { slice_array& v_even =
Stroustrup C++ 第 4 版第 197 页有以下示例,这些示例导致打印的字符流递增,直到 null 而不是 "abc"。问题:如何评估 ++*p? ++ 和 * 是相同的优先级和从右到左的
以下示例来自 Stroustrup C++ 4th Ed。第 519 页。这是我从 K&R C 和更早的 Stroustrup 书中的理解,即原始类型的局部自动变量 未定义或未知被初始化 .原始类型的
我正在使用 Stroustrup C++ 第 4 版第 1182 页的这个随机数示例。编译器在 auto 行报告错误。 ,说明 auto 不能与类的非静态成员一起使用。我对这种绑定(bind)的结果类
“没有匹配的成员函数push_back” 在 Bjarne 的原始版本中,C 的 vector 是这样写的 vector*> res; 但是这个 Value_type 模板不能正常工作所以我只是用 C
请引用B.Stroustrup的《TCPL》第4版41.2.2 Instruction Reordering部分,我抄录如下: To gain performance, compilers, opti
我在 YouTube 上看到了这个视频:https://www.youtube.com/watch?v=YQs6IC-vgmo其中 Bjarne 说最好使用 vector ,而不是链表。我无法掌握全部
在 C++ 之旅的第 83 页,Stroustrup 提供了以下正则表达式: R"((\ew+))" 书中没有任何地方定义\e,我在互联网上也找不到任何相关信息。\e 在正则表达式中代表什么? 最佳答
目前我正在阅读 Stroustrup Programming: Principles and Practice C++。我遇到了这个例子: typedef void (*Pfct0)(struct S
以下幻灯片来自 Bjarne Stroustrups 的演讲“C++ 的本质”: 我是否通过以下简单示例(值、原始指针、资源成员/子对象的智能指针)理解了他的技术?: 1. class foo{
我正在阅读 Bjarne Stroustrup “The C++ programming language” 一书,其中提到在变量定义中使用 auto 的原因之一是: The definition i
我决定自己学习 C++,我已经有了一些 Java 经验。 在 Bjarne Stroustrup 的书中他说: Prompt the user to enter the age of the reci
我正在查看 Straustrup 的 hash_map 实现。这将给出他如何实现的直觉 template, class EQ = equal_to, class A = allocator > cla
我正在使用 Stroustrup 的天鹅书。我在从 a 获取输出时遇到了问题 vector 。我遵循了 sec 的文本示例。 4.6.3 第 121 页。设法编译了源代码并能够执行它。后输入由空格分隔
在 Stroustrup 的书 Programming with C++ page 102 中,他使用 cin 读入 double 后跟 char,用于将厘米转换为英寸. 他使用的代码: cin >>
我正在阅读 Bjarne S 的 C++ 编程语言。 在第 77 页,第 4.8 节中,我发现了这一点: "枚举器可以用常量表达式初始化(§C.5) 的整数类型 (§4.1.1)。这枚举的范围包含向上
Stroustrup 提供了一个 Can_copy template .它是如何工作的? template struct Can_copy { static void constraints(
我是一名优秀的程序员,十分优秀!