- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
如何检查重载分辨率集?
我在多个调用站点中使用了 4 个相互竞争的函数。在一个调用站点中,我期望调用一个函数,但编译器会选择另一个函数。我不知道为什么/这不是微不足道的。为了了解发生了什么,我使用 enable_if/disable_if
来打开/关闭功能,但这真的很慢/乏味/烦人。
所以我希望编译器告诉我“为什么?”。也就是说,对于这个单一的调用站点:
不需要有关访问控制的信息。
基本上,我希望用 #pragma
或类似的 (__builtin
...) 标记调用站点。但 libclang 也是一种选择。
我可以使用 tip-of-trunk clang 和 gcc,但可以在必要时安装其他编译器/工具。
最佳答案
我可以想象写一个 clang插件检查正在调用的函数以及重载集中的其他函数。不过,我认为跟踪查找规则并找出为什么丢弃重载集中的候选函数以及为什么选择的函数是重载集中的最佳候选函数是完全不同的。
我还没有玩过确定重载集等。但是,下面是一个简单的起点:一个 clang 插件,如果具有特定名称的函数(当前硬编码为 "foo"
) 被找到。它还会打印找到的重载。
我正在编译代码并使用命令运行它(显然,这些存储在 make
文件中):
/opt/llvm-debug/bin/clang -I/usr/include/c++/4.2.1 -I/opt/llvm-debug/include -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS -g -fno-exceptions -fno-rtti -c -o MacOS/overloads.o overloads.cpp
/opt/llvm-debug/bin/clang -L/opt/llvm-debug/lib -Wl,-undefined,dynamic_lookup -dynamiclib -o MacOS/overloads.dylib MacOS/overloads.o
/opt/llvm-debug/bin/clang -cc1 -load MacOS/overloads.dylib -plugin overloads -plugin-arg-overloads argument -fexceptions tst.cpp
使用的 clang 版本是使用调试信息构建的:否则它似乎找不到调试符号。我可能应该了解如何直接构建工具而不是在 clang 中运行。
#include <clang/Frontend/FrontendPluginRegistry.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Lex/Preprocessor.h>
#include <clang/Lex/PPCallbacks.h>
#include <clang/AST/ASTConsumer.h>
#include <clang/AST/AST.h>
#include <clang/AST/RecursiveASTVisitor.h>
#include <clang/Sema/Sema.h>
#include <clang/Sema/Lookup.h>
#include <llvm/Support/raw_ostream.h>
#include <string>
using namespace clang;
using namespace llvm;
typedef clang::CompilerInstance CI;
typedef clang::DeclGroupRef DGR;
typedef clang::DiagnosticsEngine DE;
// ----------------------------------------------------------------------------
namespace
{
struct Consumer: clang::ASTConsumer
{
Consumer(CI& c, std::string const& name): c_(&c), name_(name) {}
bool HandleTopLevelDecl(clang::DeclGroupRef DG);
CI* c_;
std::string name_;
};
}
// ----------------------------------------------------------------------------
struct Visitor: RecursiveASTVisitor<Visitor>
{
CI* c_;
std::string name_;
Visitor(CI* c, std::string const& name): c_(c), name_(name) {}
bool VisitCallExpr(CallExpr* d);
};
bool Visitor::VisitCallExpr(CallExpr* c) {
FunctionDecl* fun = c->getDirectCallee();
if (fun && fun->getNameAsString() == this->name_) {
SourceLocation w(c->getExprLoc());
DE &de(this->c_->getDiagnostics());
int id = de.getCustomDiagID(DE::Warning, "function call: %0");
int info = de.getCustomDiagID(DE::Note, "function called");
DiagnosticBuilder(de.Report(w, id))
<< fun->getNameAsString()
;
DiagnosticBuilder(de.Report(fun->getLocStart(), info))
<< fun->getNameAsString()
;
Sema& sema = this->c_->getSema();
LookupResult result(sema, fun->getDeclName(), w, Sema::LookupOrdinaryName);
DeclContext* context = fun->getDeclContext();
if (sema.LookupName(result, sema.getScopeForContext(context))) {
int over = de.getCustomDiagID(DE::Note, "function overload");
LookupResult::Filter filter = result.makeFilter();
while (filter.hasNext()) {
DiagnosticBuilder(de.Report(filter.next()->getLocStart(), over))
;
}
filter.done();
}
}
//else {
// // I think the callee was a function object or a function pointer
//}
return true;
}
void doDecl(Consumer* c, Decl* d) {
Visitor(c->c_, c->name_).TraverseDecl(d);
}
// ----------------------------------------------------------------------------
bool Consumer::HandleTopLevelDecl(DeclGroupRef DG) {
std::for_each(DG.begin(), DG.end(),
std::bind1st(std::ptr_fun(&doDecl), this));
return true;
}
// ----------------------------------------------------------------------------
namespace
{
class Plug
: public clang::PluginASTAction
{
protected:
ASTConsumer*
CreateASTConsumer(CompilerInstance& c, llvm::StringRef);
bool ParseArgs(clang::CompilerInstance const&,
std::vector<std::string> const&) {
return true;
}
};
}
ASTConsumer*
Plug::CreateASTConsumer(CompilerInstance& c, llvm::StringRef) {
return new Consumer(c, "foo");
}
static clang::FrontendPluginRegistry::Add<Plug>
registerPlugin("overloads", "report overloads of a function at a call");
代码不是很漂亮,并没有真正按照您的要求进行。但是,更好地格式化函数声明,可能使用 Sema
对象调查一下为什么它不匹配等可以使代码合理地接近您正在寻找的工具,我想想。
关于c++ - 如何检查给定调用站点的重载解决方案集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20092672/
最近我用 php 建立了一个网站,但他们的旧网站都是 HTML 格式的。所以现在我不知道使用 .htaccess 将所有这些旧链接重定向到新站点(可能将所有带有 HTML 的链接重定向到主域)的最佳方
我创建了一个新的 WordPress 网站,它是我旧网站的更名版本。它有一个新的域和一个新的设计。除此之外,其他一切都是相同的,包括内容和链接结构。现在我想完全重定向旧链接。与旧帖子一样,标签和类别
我想使用 WatiN测试我正在开发的网站的功能。理想情况下,我会在测试开始运行之前以编程方式部署网站 (asp.net MVC3),然后在每次测试之前刷新数据。这可能吗? 最佳答案 在此处阅读有关使用
我们的网站使用我们自己定制的 session 状态管理,与 ASP.NET session 状态分开。但是由于少数特殊页面使用 SQL Server Reporting Services,我们还需要启
不久前我看到一个网站,其中有 JavaScript/HTML/CSS 栏目,下面有实际代码的样子。有点像 jsFiddle,但它有用户示例和演示。有谁知道这个网站的名字吗?我到处都找不到它!谢谢! 最
我们的核心数据库出现问题,该数据库已由前一天的备份数据库恢复。 此后,网站工作正常,但我们在发布任何更改时遇到问题。一旦点击发布按钮,“发布正在初始化..”消息就会持续很长时间。截至“发布开始/结束”
我们的核心数据库出现问题,该数据库已由前一天的备份数据库恢复。 此后,网站工作正常,但我们在发布任何更改时遇到问题。一旦点击发布按钮,“发布正在初始化..”消息就会持续很长时间。截至“发布开始/结束”
Maven 不仅仅是一款项目构建和依赖管理工具,它还能够聚合项目信息,促进项目团队间地交流。POM 中可以包含各种项目信息,例如:项目描述、SCM 地址、许可证信息,开发者信息等。用户可以使用 Mav
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭上个月。 Improve this ques
您知道哪些网站正在使用 Silverlight?此信息将帮助我们决定何时采用 Silverlight 平台。 这些网站应该是面向公众的并且被认为是高流量的。 (每月至少 300,000 次点击)。 我
我尝试通过 VS2017 中的发布上下文菜单将我的 .net 核心站点部署到 Azure,偶尔(大约三分之一的部署),我收到以下错误。 Web deployment task failed. (Web
我为 20 个不同的国家/地区创建了一个包含 20 个站点(每个站点一个不同的域)的 Django 项目。这些站点共享所有内容:代码库、数据库、网址、模板等。 他们唯一不共享的是我在每个站点设置文件中
如何将商店页面添加到我使用 jekyll 和基础构建的网站? 任何自动化平台/方法都可以做到这一点。 谢谢。 最佳答案 您可以使用 snipcart .有一个blog post和一个 demo sit
我部署了一个服务结构集群,域为 foo.northcentralus.cloudapp.azure.com 它具有单一节点类型和单一公共(public) IP 地址/负载均衡器。 假设我部署了以下
我不是一个大的typ3 专家,也无法访问我正在使用的typ3 实例中的typoscript 选项(这是一个非常大的站点,我没有这样做的授权)。所以我希望这个问题适合 stackoverflow(如果没
我们正在对我们的 Drupal 站点进行性能调整。 我们正在使用 Siege 来衡量性能(作为 drupal 访问者)。 环境: Nginx + FastCGI + Memcache Siege 运行
我搜索了 SO、SU 和 SP.SE寻求解决方案,但找不到我需要的东西。我正在寻找一个解决方案,它可能是一个脚本或一些其他非编码方法/工具。 我正在尝试编写一个脚本(供其他人使用)或某种其他形式的自动
我有一个 Django 站点,它使用本地化中间件与 gettext 和 trans/blocktrans 模板标签相结合,根据用户代理字符串中的首选语言向访问者显示不同的页面(这似乎是在 Django
我是 Drupal 新手。是否可以设置所有内容并在服务器上部署 Drupal?我的意思是像放入内容、设置模块等...,然后将它们全部放到生产服务器上? 最佳答案 当然。 复制所有文件 编辑数据库凭证(
我想将以下行添加到我的 head.html仅在运行时 jekyll serve本地: 如果可能的话,我正在考虑使用一些简单的液体检查。 最佳答案 当你做 jekyll serve本地默认 {{
我是一名优秀的程序员,十分优秀!