这是 this 的后续题。
#include <iostream>
struct type1 {};
struct type2 {};
void foo(type1 x)
{
std::cout << "foo(type1)" << std::endl;
}
template<typename T>
void bar() {
foo(T());
}
int main()
{
bar<type1>();
bar<type2>();
return 0;
}
void foo(type2 x)
{
std::cout << "foo(type2)" << std::endl;
}
在上面的代码中foo(type2)在 bar<type2> 的实例化时不可见在 main .然而代码编译并产生以下输出:
foo(type1)
foo(type2)
编译器如何知道 foo(type2)实例化时可用 bar<type2>在 main ?
编辑:我试图更多地了解模板实例化过程中的重载解析是如何工作的。考虑下面的代码:
#include <iostream>
struct type1 {};
struct type2 {};
struct type3 {
operator type2() { return type2(); }
};
void foo(type1 x)
{
std::cout << "foo(type1)" << std::endl;
}
void foo(type2 x)
{
std::cout << "foo(type2)" << std::endl;
}
int main()
{
foo(type3());
return 0;
}
void foo(type3 x)
{
std::cout << "foo(type3)" << std::endl;
}
输出是
foo(type2)
即使是更接近的比赛foo(type3)有空,电话foo(type3())解析为 foo(type2)因为在那之前,这是唯一被编译器解析过的候选者。现在考虑以下代码:
#include <iostream>
struct type1 {};
struct type2 {};
struct type3 {
operator type2() { return type2(); }
};
void foo(type2 x)
{
std::cout << "foo(type2)" << std::endl;
}
template<typename T>
void bar() {
foo(T());
}
int main()
{
bar<type3>();
return 0;
}
void foo(type3 x)
{
std::cout << "foo(type3)" << std::endl;
}
输出是
foo(type3)
也就是调用点bar<type3>() , 即使只有 foo(type2)可见,编译器仍然选择 foo(type3)稍后出现,因为这是更接近的匹配。
最佳答案
任何没有定义的符号都将在链接过程中被替换,因为函数 foo(type2)可以在另一个文件中提供。
编译器会在整个过程结束时说出所需的函数是否已定义,此时无法应用进一步的替换。
为了阐明理解,您必须了解编译(例如,一个普通的 C 程序)所需的步骤:
首先,展开代码中的所有宏;
然后根据语言语法验证您的代码,以便将其转换为汇编语言——编译过程本身;在此步骤中,找到的每个没有定义的符号都会在表中用条目 (symbol, definition) 进行注释, 稍后将完成,以便正确构建您的程序;
接下来,您编译成汇编的代码将被转换为机器语言,即创建对象;
最后,您需要链接您已经可执行的对象,以解决对符号定义的任何依赖;最后一步检查您的对象是否有 undefined symbol ,添加来自其他模块或库的定义,从而完成程序。
如果任何符号没有正确“链接”到它的定义,编译器将指出程序中的错误——经典的 undefined reference to... .
考虑到您发布的代码,该过程将一直执行到它到达编译器为止。编译器会遍历代码,注意 type1 的定义, type2 , foo(type1 x) , 和 bar<T>() .
struct type1 {};
struct type2 {};
当它到达 main 时,它会找到对 bar<type1>(); 的调用, 并会调用 foo(type1()) ,这是已知的,可以正确使用。
void foo(type1 x) {
std::cout << "foo(type1)" << std::endl;
}
template<typename T>
void bar() {
foo(T());
}
int main() {
bar<type1>();
bar<type2>();
return 0;
}
一旦到达下一个电话,bar<type2>(); , 它会尝试调用 foo(type2()) , 但没有这样的定义可供使用,因此它将此调用关联为未知符号,必须在以后的过程中用定义替换。
编译器运行完main之后,它达到了一个新的定义,这正是正在创建的“翻译表”上缺少定义的定义。
void foo(type2 x) {
std::cout << "foo(type2)" << std::endl;
}
因此,在下一步中,编译能够用其各自的定义替换符号,并且程序可以正确编译。
问候!
关于c++ - 重载解析解析为一个尚不可见的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13485132/
我有一个字符串input="maybe(thisis|thatwas)some((nice|ugly)(day|night)|(strange(weather|time)))"Ruby中解析该字符串的最佳方法是什么?我的意思是脚本应该能够像这样构建句子:maybethisissomeuglynightmaybethatwassomenicenightmaybethiswassomestrangetime等等,你明白了......我应该一个字符一个字符地读取字符串并构建一个带有堆栈的状态机来存储括号值以供以后计算,还是有更好的方法?也许为此目的准备了一个开箱即用的库?
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>
我正在使用ruby1.9解析以下带有MacRoman字符的csv文件#encoding:ISO-8859-1#csv_parse.csvName,main-dialogue"Marceu","Giveittohimóhe,hiswife."我做了以下解析。require'csv'input_string=File.read("../csv_parse.rb").force_encoding("ISO-8859-1").encode("UTF-8")#=>"Name,main-dialogue\r\n\"Marceu\",\"Giveittohim\x97he,hiswife.\"\
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?