- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我遇到了以下涉及 ADL 和已删除函数的令人困惑的示例:
第一个例子:
namespace A
{
struct S{};
void f(S){cout << "adl" << endl;}
}
namespace C
{
//void f() = delete;
//void f (double);
void test()
{
A::S arg;
f(arg);
}
}
int main()
{
C::test();
return 0;
}
正如预期的那样,A::f
是通过 ADL 调用的。在下一个示例中,C
中有一个已删除的同名函数:
namespace A
{
struct S{};
void f(S){cout << "adl" << endl;}
}
namespace C
{
void f() = delete;
//void f (double);
void testi()
{
A::S arg;
f(arg);
}
}
int main()
{
C::testi();
return 0;
}
编译失败并显示错误消息 error: use of deleted function 'void C::f()'
。显然,删除的函数阻止了 ADL 版本进入重载表。现在来看最后一个例子:除了已删除的函数之外,现在还有另一个未删除的同名函数:
namespace A
{
struct S{};
void f(S){cout << "adl" << endl;}
}
namespace C
{
void f() = delete;
void f (double);
void testi()
{
A::S arg;
f(arg);
}
}
int main()
{
C::testi();
return 0;
}
运行它会执行 f
的 ADL 版本。所以总而言之:
我的问题:这种行为是故意的吗?如果是这样,标准的哪一部分规定了这一点?
编辑:忘了说我用的是onlinegdb编译,所以用的是gcc/g++。
最佳答案
明确的编译器错误。引用 C++ 标准草案 (n4659) [dcl.fct.def.delete]/2 :
A program that refers to a deleted function implicitly or explicitly, other than to declare it, is ill-formed. [ Note: This includes calling the function implicitly or explicitly and forming a pointer or pointer-to-member to the function. It applies even for references in expressions that are not potentially-evaluated. If a function is overloaded, it is referenced only if the function is selected by overload resolution. The implicit odr-use of a virtual function does not, by itself, constitute a reference. — end note ]
重载解析无法通过 ADL 选择该重载。所以这个函数不应该被引用。第二个代码示例是一个格式良好的 C++ 程序(如果丢失的包含指令被放回原处)。
你提到用g++编译,注意这个issue是fixed in GCC 7.2.0
关于c++ - 删除的函数和 ADL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46314709/
这个问题在这里已经有了答案: How do I write an ADL-enabled trailing return type, or noexcept specification? (4 个答
例如,我想使用约束来确保函数 isinf为模板参数实现 T .如 T是 float 之一, double , long double或整数类型,这可以通过以下方式完成: #include templ
我在 ADF 中进行自定义事件,其中涉及从 Azure 存储 Blob 读取多个文件,对它们进行一些处理,然后最终将生成的文件写入 Azure Data Lake Store。最后一步是我停止的地方,
我遇到了以下涉及 ADL 和已删除函数的令人困惑的示例: 第一个例子: namespace A { struct S{}; void f(S){cout << "adl" << end
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
template struct S { bool valid(T a) { return is_valid(a); } }; bool is_valid(int) { return t
我有几个命名空间,每个命名空间都有一个名为 f 的函数模板。 // f() and Widget namespace A { struct Widget { }; template vo
我有一个带有类内定义友元函数的类,我最好不要修改它(它来自已经部署的 header ) #include #include namespace our_namespace { template
我有一个函数模板 printSize 在声明 getSize 之前调用重载函数 getSize。为了让我的程序结构更清晰,我想将两个函数放在不同的命名空间 A 和 B 中,如注释代码行所示。但是,AD
struct S { vector v; void method() { begin(v); } }; 上面的代码片段编译正常,因为 ADL直到我添加
对于非限定名称查找,“通常非限定名称查找”和“参数相关名称查找”(ADL),我无法在标准中找到哪一个先发生? 再次尝试向过载候选集添加一些内容,但顺序似乎并不重要。但仍然很高兴知道哪一个先发生。 谢谢
我试图了解如何 ADL至少它的基础是有效的,并创建了以下代码: #include #include #include using std::pair; using std::string; u
简而言之,我试图了解 C++ 中参数依赖查找的行为。我不清楚 ISO/IEC 14882:2017 (E) 中关于 ADL 的一些陈述。我希望有人能向我澄清它们。 按照标准, Typedef name
可以给我一个不使用模板的 ADL 示例吗?从来没有见过这样的东西。我的意思是像 here .具体来说,我对导致上述一些陷阱的示例感兴趣。 编辑: 我认为 Tomalak 的回答可以延伸到陷阱。考虑一下
有3个例子: 我. typedef int foo; namespace B { struct S { operator int(){ return 24; }
我正在使用 Visual Studio 2010 在 Microsoft Windows 7 上编译 x64 服务,使用 Boost variant像这样的东西: namespace my_ns {
情况是某些成员函数 bar::Bar::frobnicate 想要利用 ADL 在具有相同名称的函数中从某个未知命名空间中查找函数。但是,它只能找到自己的名字。 测试用例 (请注意,实际上,Bar 是
我对 C++ 中的标准 ADL 解析有疑问。 这是解释我的查询的示例代码: #include // The mechanism: namespace A { template ::std::st
这是来自 Does argument dependent lookup only search namespaces or classes too? 的跟进问题,其中@David Rodríguez
这个问题在这里已经有了答案: Why doesn't ADL find function templates? (4 个答案) 关闭 8 年前。 我想明白为什么调用模板f下面不编译: struct
我是一名优秀的程序员,十分优秀!