- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
namespace N {
class C {};
template<typename X>
char const * found(X && x) {
return "found";
}
template<typename, typename X>
char const * notfound(X && x) {
return "not found";
}
}
这定义了一个命名空间 N
上课 C
和两个功能模板。 found
有一个模板参数,可以从函数参数中推导出来。 notfound
有一个无法推导的附加模板参数。
给定以下测试代码(on ideone):
#include <iostream>
int main() {
N::C object;
std::cout
<< found(object) << std::endl
<< notfound<bool>(object) << std::endl // ERROR
<< notfound<bool, N::C>(object) << std::endl; // ERROR
}
我假设 argument dependent lookup会同时找到 found
和 notfound
通过参数类型 N
的最内层封闭命名空间(即 N::C
) .
但是:
prog.cpp: In function ‘int main()’:
prog.cpp:21:6: error: ‘notfound’ was not declared in this scope
<< notfound<bool>(object) << std::endl
^~~~~~~~
prog.cpp:21:6: note: suggested alternative:
prog.cpp:12:15: note: ‘N::notfound’
char const * notfound(X && x) {
^~~~~~~~
(注释掉 notfound<bool, N::C>(object)
调用后 notfound<bool>(object)
的相同错误)
为什么是notfound
没有通过 ADL 找到?
背景:我正在实现 get
一些包装类的函数,总的来说与 std::get(std::tuple)
比较相似.包装类,作为一个实现细节,存在于某个命名空间 lib::aspect::part::impl
中。 .我不希望图书馆的用户指定 using lib::aspect::part::impl::get
出于显而易见的原因。
最佳答案
因为对带有显式指定模板参数的函数模板的函数调用要求模板的名称必须通过普通查找找到;直到那个ADL踢不进去。
来自标准:$17.8.1/8 Explicit template argument specification [temp.arg.explicit]
(强调我的)
[ Note: For simple function names, argument dependent lookup applies even when the function name is not visible within the scope of the call. This is because the call still has the syntactic form of a function call ([basic.lookup.unqual]). But when a function template with explicit template arguments is used, the call does not have the correct syntactic form unless there is a function template with that name visible at the point of the call. If no such name is visible, the call is not syntactically well-formed and argument-dependent lookup does not apply. If some such name is visible, argument dependent lookup applies and additional function templates may be found in other namespaces. [ Example:
namespace A {
struct B { };
template<int X> void f(B);
}
namespace C {
template<class T> void f(T t);
}
void g(A::B b) {
f<3>(b); // ill-formed: not a function call
A::f<3>(b); // well-formed
C::f<3>(b); // ill-formed; argument dependent lookup applies only to unqualified names
using C::f;
f<3>(b); // well-formed because C::f is visible; then A::f is found by argument dependent lookup
}— end example ] — end note ]
最后一句给出了一个可能的解决方法;您可以在任何地方添加函数模板的声明,以使名称可见以便被调用。例如
template<typename>
void notfound();
int main() {
N::C object;
std::cout
<< found(object) << std::endl
<< notfound<bool>(object) << std::endl
<< notfound<bool, N::C&>(object) << std::endl; // btw the 2nd template argument should be N::C&
}
关于c++ - 对于具有附加(非推导)模板参数的函数,ADL 失败(或未完成?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44490086/
运行以下代码片段,我没有收到任何错误,并且得到了预期的结果。但是,由于第二个模板实例化是不明确的 ( both type specifiers are references ),我担心这可能不是定义的
考虑以下示例: #include struct A {}; template void f() { static_assert(std::is_same_v); // #1 A&
从 DH 协商中派生的 secret 派生出一个比方说 128 位 AES key 的正确(可接受的)方法是什么? 使用前 128 位 对 secret 进行哈希处理并使用前 128 位 使用一些更复
从 DH 协商中派生的 secret 派生出一个比方说 128 位 AES key 的正确(可接受的)方法是什么? 使用前 128 位 对 secret 进行哈希处理并使用前 128 位 使用一些更复
我对编写模板元编程比较陌生,这可能是我找不到解决这个问题的原因。问题是这样的:我正在开发一个数学库,它有很多函数,比如确定整数或 std::initializer_list 的质数,将整数更改为罗马数
我在 Android Oreo 源代码中阅读了一些我不太理解的代码。 首先,类IOMXNode有一个函数: class IOMXNode : public IInterface { public: +
有很多关于模板参数推导的讨论和澄清,特别是引用折叠和“通用引用”。本题通过相关细节:How does auto deduce type? ,而 Scott Meyers 的这篇论文更详细,可能会提供更
我将 Java 中的许多假设带到了我对 C++ 的学习中,这似乎再次难倒了我。我没有足够的词汇量来 Eloquent 地说出我希望从以下程序中看到什么,所以我只展示它并说出我希望看到的内容: #inc
对于下面的程序,Clang 5 (trunk) 报告 IsNoexcept 不可推导,而 GCC 7.1 会出现段错误。 标准(草案)对此有何评论?这是编译器 QOI 问题吗? static_asse
我最近发现,在 lambda 中按值捕获 const 对象意味着 labmda 主体(即 lambda 的数据成员)内的变量也是 const. 例如: const int x = 0; auto fo
我是否有机会推断出 PHP Closure 参数类型信息?考虑这个例子: 5, 'b' => 10]); } else { call_user_func($closure, 5, 10);
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
对于上述 svm 的拉格朗日函数,我可以得到如下的偏导数: 但是,我不明白如何将它们插入拉格朗日以导出对偶形式? W可以被替换,但是b去哪里了? 有人可以解释一下并给出详细步骤吗? 最佳答案 你的拉格
我正在寻找一些算法、程序或函数来推断变量的创建方式,只要我提供其他变量即可。我认为计算机程序员会称之为“反编译”,而架构师会称之为“逆向工程”,但我想我不知道统计学家会怎么调用它......或者是否有
这就是我的简单类的样子。 template class A { T first; T second; public: A(T f, T s) : first(f), second(s) {}; te
这个问题在这里已经有了答案: Is it possible to figure out the parameter type and return type of a lambda? (5 个答案)
我有一个函数需要两个 std::function s 作为参数。第二个函数的参数与第一个函数的结果类型相同。 我写了一个这样的函数模板: template void examplFunction(st
O'reilly Optimizing SQL Statments Book的Explaining MySQL Explain章节,最后有这个问题。 The following is an examp
举例 template void function(T&& arg) 有人可以详细解释它是如何结束函数签名变成左值的 T& 和传入的右值的 T&& 吗?我知道不知何故(需要标准行)T -> T& 在
我正在开发用于 EMV 交易的软件,但我面临着雇佣我的公司的文档严重缺乏的问题。 其中之一是关于用于生成 ARQC 的 MKD(在第一个 GENERATE AC 期间)。我从消息请求中知道IAD如下:
我是一名优秀的程序员,十分优秀!