我一直认为当我使用初始化列表 C++ 语法时:
something({ ... });
编译器总是很清楚我想调用采用 std::initializer_list 的重载,但对于 MSVC 2015 似乎不太清楚。
我测试了这个简单的代码:
#include <cstdio>
#include <initializer_list>
namespace testing {
template<typename T>
struct Test {
Test() {
printf("Test::Test()\n");
}
explicit Test(size_t count) {
printf("Test::Test(int)\n");
}
Test(std::initializer_list<T> init) {
printf("Test::Test(std::initializer_list)\n");
}
T* member;
};
struct IntSimilar {
int val;
IntSimilar() : val(0) {}
IntSimilar(int v) : val(v) {}
operator int() {
return val;
}
};
}
int main() {
testing::Test<testing::IntSimilar> obj({ 10 });
return 0;
}
在 GCC 6.3 中,它按预期工作,调用 Test::Test(std::initializer_list)
但在 MSVC 2015 中,此代码调用 Test::Test(int)。
似乎 MSVC 可以以某种方式忽略 {} 并选择一个无效/意外的重载来调用。
标准对这种情况怎么说?哪个版本有效?
有人可以对此进行测试并确认此问题是否仍然存在于 MSVC 2017 中吗?
最佳答案
Which version is valid?
根据我对标准的理解,GCC 是对的。
What does standard says about this situation?
你在写作时做了什么Test obj1({10});是direct-initializing Test 类型的对象用表达式 { 10 } .在重载决议期间,编译器必须决定调用哪个构造函数。根据16.3.3.2 § 3 (3.1.1) [over.ics.rank] :
list-initialization sequence
L1is a better conversion sequence than list-initialization sequenceL2ifL1converts tostd::initializer_list<X>for someXandL2does not [...]
标准也提供了例子
void f1(int); // #1
void f1(std::initializer_list<long>); // #2
void g1() { f1({42}); } // chooses #2
这是 VS 和 clang 与 GCC 的不同之处:虽然在这个特定示例中所有三个都会产生相同的结果,但将其更改为
#include <iostream>
struct A { A(int) { } };
void f1(int) { std::cout << "int\n"; } // #1
void f1(std::initializer_list<A>) { std::cout << "list\n"; } // #2
int main() {
f1({42});
}
会让clang chose the int -constructor ,提示文字 42 周围不必要的大括号(由于遗留原因,这似乎只是在标准中,请参阅 here )而不是检查 { 42 } 是否存在。列表序列确实无法转换为std::initializer_list<A> .
但是请注意写作 Test obj1{ 10 };会导致不同的评价:根据list-initialization的规则:
- Otherwise, the constructors of T are considered, in two phases:
- All constructors that take std::initializer_list as the only argument, or as the first argument if the remaining arguments have default values, are examined, and matched by overload resolution against a single argument of type std::initializer_list
所以 initializer_list仅考虑 initializer_list 的构造函数被用于特殊的重载解决阶段应用正常重载决议之前的构造函数,如著名的 std::vector 中所示-陷阱:
// will be a vector with elements 2, 0 rather than a vector of size 2 with values 0, 0
std::vector<int> v{ 2, 0 };
事实上,在这两种情况下,标准都决定使用 initializer_list constructor 是一个一致的选择,但从技术上讲,选择它的原因在幕后是完全不同的。
关于c++ - 从括号内的 initializer_list 构造时调用了错误的重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47608388/
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
在rails源中:https://github.com/rails/rails/blob/master/activesupport/lib/active_support/lazy_load_hooks.rb可以看到以下内容@load_hooks=Hash.new{|h,k|h[k]=[]}在IRB中,它只是初始化一个空哈希。和做有什么区别@load_hooks=Hash.new 最佳答案 查看rubydocumentationforHashnew→new_hashclicktotogglesourcenew(obj)→new_has
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file
我克隆了一个rails仓库,我现在正尝试捆绑安装背景:OSXElCapitanruby2.2.3p173(2015-08-18修订版51636)[x86_64-darwin15]rails-v在您的Gemfile中列出的或native可用的任何gem源中找不到gem'pg(>=0)ruby'。运行bundleinstall以安装缺少的gem。bundleinstallFetchinggemmetadatafromhttps://rubygems.org/............Fetchingversionmetadatafromhttps://rubygems.org/...Fe
在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee
我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie
我正在阅读SandiMetz的POODR,并且遇到了一个我不太了解的编码原则。这是代码:classBicycleattr_reader:size,:chain,:tire_sizedefinitialize(args={})@size=args[:size]||1@chain=args[:chain]||2@tire_size=args[:tire_size]||3post_initialize(args)endendclassMountainBike此代码将为其各自的属性输出1,2,3,4,5。我不明白的是查找方法。当一辆山地自行车被实例化时,因为它没有自己的initialize方法